TableViewCell的多选

作者: 施主小欣 | 来源:发表于2016-12-07 17:32 被阅读85次

最近项目告一段落了,发现自己工程里面的cell多选其实用的蛮多的,为了记录一下自己的日常,也为了方便新入门没有思路的小白们做个参考,我就厚颜无耻的分享下自己的代码~ 😁

首先肯定是要创建一个数组用来盛装选中的cell

@property(nonatomic,strong)NSMutableArray *selecArray;//选择的数组

然后在cellForRow的方法里面做处理 因为项目需要所以我做成了下面的效果

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  MemBerAssignedFornallyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MemBerAssignedFornallyTableViewCell"];

  MyVipModel *model = self.dataSource[indexPath.row];
  cell.model = self.dataSource[indexPath.row];
  cell.jx_separatorStyle = JXSeparatorStyleNone;

  if ([self.selecArray containsObject:model.Id]) {
      cell.checkImageView.image = [UIImage imageNamed:@"圆圈选中"];
    }else {
      cell.checkImageView.image = [UIImage imageNamed:@"圆待选"];
  }

  return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indePath {
  [tableView deselectRowAtIndexPath:indexPath animated:NO];
  MyVipModel *model = self.dataSource[indexPath.row];
  if([self.selecArray containsObject:model.Id]) {
    [self.selecArray removeObject:model.Id];
  }else {
    [self.selecArray addObject:model.Id];
  }
}
//cell刷新
   NSIndexPath*index = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
  [tableView reloadRowsAtIndexPaths:[NSArray array arrayWithObjects:index,nil] withRowAnimation:UITableViewRowAnimationNone];
}
Simulator Screen Shot 2016年12月7日 下午5.06.26.png

也可以是下面这样的

IMG_0992.PNG

如果想用cell自带的样式✓
在cellForRow里面写成

//数组中包含当前行号,设置对号
if ([self.selectArray containsObject:model.Id]) {
    
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else {
    
    cell.accessoryType = UITableViewCellAccessoryNone;
}

相关文章

网友评论

    本文标题:TableViewCell的多选

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