美文网首页
2020-07-24 ios tableview 单选

2020-07-24 ios tableview 单选

作者: 缘來諟夢 | 来源:发表于2020-07-24 15:41 被阅读0次

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];

相关文章

网友评论

      本文标题:2020-07-24 ios tableview 单选

      本文链接:https://www.haomeiwen.com/subject/grojlktx.html