1.聲明一個數(shù)組(用來放選中的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