解决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;
}
?
轉載于:https://www.cnblogs.com/hw140430/p/3749039.html
總結
以上是生活随笔為你收集整理的解决cell循环利用造成的重复勾选的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 短信转彩信限制页数
- 下一篇: JavaScript高级程序设计20.p