UITableView的基本使用 cell的单选和多选

<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;

//用来记录我们点击的indexpath

@property(nonatomic,assign)NSIndexPath *isSelected;


//创建tableview

_tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 238, WEIDTH, HEIGHT-235-67) style:UITableViewStylePlain];

    _tableView.delegate=self;

    _tableView.dataSource=self;

    _tableView.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];

    _tableView.rowHeight=80;

    [self.view addSubview:_tableView];

//注册我们自定义的cell

    [self.tableView registerClass:[StatusTableViewCell class] forCellReuseIdentifier:NSStringFromClass([StatusTableViewCell class])];


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 4;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    StatusTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:NSStringFromClass([StatusTableViewCell class])];

    if (indexPath.row==0) {

        cell.nameLabel.text=@"";

        cell.dataLabel.text=@"";

        cell.timeLabel.text=@"";

        cell.numberLabel.text=@"";

        cell.bgView.backgroundColor=[UIColor colorWithRed:158/255.0 green:222/255.0 blue:89/255.0 alpha:1];

        cell.HeadimageView.image=[UIImage imageNamed:@"turang"];

    }

    if (indexPath.row==1) {

        cell.nameLabel.text=@"";

        cell.dataLabel.text=@"";

        cell.timeLabel.text=@"";

        cell.numberLabel.text=@"";

        cell.bgView.backgroundColor=[UIColor colorWithRed:245/255.0 green:113/255.0 blue:112/255.0 alpha:1];

        cell.HeadimageView.image=[UIImage imageNamed:@"yanfen"];

    }

    if (indexPath.row==2) {

        cell.nameLabel.text=@"";

        cell.dataLabel.text=@"";

        cell.timeLabel.text=@"";

        cell.numberLabel.text=@"";

        cell.bgView.backgroundColor=[UIColor colorWithRed:122/255.0 green:179/255.0 blue:249/255.0 alpha:1];

        cell.HeadimageView.image=[UIImage imageNamed:@"shuiwexi"];

    }

    if (indexPath.row==3) {

        cell.nameLabel.text=@"";

        cell.dataLabel.text=@"";

        cell.timeLabel.text=@"";

        cell.numberLabel.text=@"";

        cell.bgView.backgroundColor=[UIColor colorWithRed:251/255.0 green:175/255.0 blue:82/255.0 alpha:1];

        cell.HeadimageView.image=[UIImage imageNamed:@"ph"];

    }

    return cell;

    

}



//单选

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:NO];


   //找cell主要可以用来单选

    

    StatusTableViewCell *celled=[tableView cellForRowAtIndexPath:_isSelected];

    celled.accessoryType=UITableViewCellAccessoryNone;


    StatusTableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];

    cell.accessoryType=UITableViewCellAccessoryCheckmark;


    self.isSelected=indexPath;

    //加上下面的就可以一个都不选,仅仅上面的就会始终有一个

//    if (_isSelected==indexPath) {

//        

//        celled.accessoryType=UITableViewCellAccessoryNone;

//    }else{

//        

//        cell.accessoryType=UITableViewCellAccessoryCheckmark;

//    }


}


//多选

//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

//    [tableView deselectRowAtIndexPath:indexPath animated:NO];

//    

//    StatusTableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];

//    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { //如果为选中状态

//        cell.accessoryType = UITableViewCellAccessoryNone; //切换为未选中

//        [_selectIndexs removeObject:indexPath]; //数据移除

//    }else { //未选中

//        cell.accessoryType = UITableViewCellAccessoryCheckmark; //切换为选中

//        [_selectIndexs addObject:indexPath]; //添加索引数据到数组

//    }    NSLog(@"indexPath==%@",_isSelected);

//}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值