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];
}
就可以啦~