2020-07-24 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 样式

GCAreaLeftCell *cell = [tableView cellForRowAtIndexPath:_selectIndex];
cell.stateLabel.hidden = YES;
cell.backgroundColor = UIColorFromRGB(0xf5f5f5);
_selectIndex = indexPath;
GCAreaLeftCell *cell1 = [tableView cellForRowAtIndexPath:indexPath];
cell1.stateLabel.hidden = NO;
cell1.backgroundColor = [UIColor whiteColor];
[tableView reloadData];