美文网首页
选中多个cell (cell 中的Button)

选中多个cell (cell 中的Button)

作者: 努努Nunu | 来源:发表于2020-04-21 18:06 被阅读0次

1.声明一个数组(用来放选中的cell)

@property (nonatomic, strong) NSMutableArray *selectIndexs;

2.*然后初始化

self.selectIndexs = [[NSMutableArray alloc] init];

3.然后在tableView的代理中这样写

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ShowAreaCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShowAreaCell"];
        if (!cell) {
            cell = [[ShowAreaCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ShowAreaCell"];
        }
    cell.nameLabel.text = self.tabArray[indexPath.row];
    cell.selectedButton.selected = [self.selectIndexs containsObject:indexPath];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.selectIndexs containsObject:indexPath]) {
        [self.selectIndexs removeObject:indexPath];
    } else {
        [self.selectIndexs addObject:indexPath];
    }
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
}

就可以啦~

! 截屏2020-04-21下午6.06.43.png

相关文章

网友评论

      本文标题:选中多个cell (cell 中的Button)

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