解决cell循环利用造成的重复勾选

@interface ProfessionViewController (){
NSMutableArray *_professionArray;//cell模型数组
NSMutableArray *_selectArray; //已选中的cell数组
}

- (void)viewDidLoad{

  //初始化已选中的cell数组
_selectArray = [NSMutableArray array];
} #pragma mark 当cell被选中时调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //获取当前选中的cell模型
Profession *profession = _professionArray[indexPath.section];
//获取当前选中的cell模型中的title
NSString *title = profession.trade[indexPath.row];
  if ([_selectArray containsObject:title]) {
//之前是选中,现在取消选中
[_selectArray removeObject:title];
}else{
//之前未选中,现在选中
[_selectArray addObject:title];
}
  //刷新被选中的cell  
  [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}

#pragma mark  设置每一行cell显示的内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

//定义循环利用标识

static NSString *identifier = @"profession";

_cell = [tableView dequeueReusableCellWithIdentifier:identifier];

//如果cell为空

if (!_cell) {

_cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier];

}

//取得cell模型数组

Profession *profession = _professionArray[indexPath.section];

//取得cell模型数组中的title

NSString *title = profession.trade[indexPath.row];

//取得行业并设置给cell

  _cell.textLabel.text = title;

//判断当前title是否在已选中的数组中

if ([_selectArray containsObject:title]) {

//打钩

_cell.accessoryType = UITableViewCellAccessoryCheckmark;

}else{

//不打钩

_cell.accessoryType = UITableViewCellAccessoryNone;

}

return _cell;

}


上一篇:Kafka:ZK+Kafka+Spark Streaming集群环境搭建(二十六)Structured Streaming:WARN clients.NetworkClient: Error while fetching metadata with correlation id 1 : {my-topic=LEADER_NOT_AVAILABLE}


下一篇:一对多关联按照一方的id查找信息的一个笛卡尔积问题