ios tableview 单选

1 声明一个标示 selectindex 包含selection和row

@property (nonatomic, assign) NSIndexPath *selectIndex;

2 设置默认选中第一个

   self.selectIndex = [NSIndexPath indexPathForRow:0 inSection:0];

    [self.tableview selectRowAtIndexPath:self.selectIndex animated:YES scrollPosition:UITableViewScrollPositionNone];

3 cell for row 判断 选中那个 selectindex 和indexpath 是否相等判断是否选中

   NSInteger row = [indexPath row];
    NSInteger oldrow = [self.selectIndex row];
    if  (row == oldrow && self.selectIndex != nil){
        [cell.selectImageView setImage:UIImageMake(@"czzhong")];
    }else{
         [cell.selectImageView setImage:UIImageMake(@"czweixuanzhong")];
        
    }

4 didselect 中判断 根据样式 更改以前选中的cell 样式 重新给selectindex 赋值 设置选中的cell 样式

  HomePaytypeViewCell *cell = [tableView cellForRowAtIndexPath:_selectIndex];
    
    [cell.selectImageView setImage:UIImageMake(@"czweixuanzhong")];
    
    _selectIndex = indexPath;
    HomePaytypeViewCell *cell1 = [tableView cellForRowAtIndexPath:indexPath];
       
       [cell1.selectImageView setImage:UIImageMake(@"czzhong")];
    
    [tableView reloadData];

你可能感兴趣的:(ios tableview 单选)