解决cell循环利用造成的重复勾选
- (void)viewDidLoad{
//初始化已選中的cell數(shù)組_selectArray = [NSMutableArray array];}#pragma mark 當(dāng)cell被選中時(shí)調(diào)用 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{//獲取當(dāng)前選中的cell模型 Profession *profession = _professionArray[indexPath.section];//獲取當(dāng)前選中的cell模型中的title NSString *title = profession.trade[indexPath.row]; if ([_selectArray containsObject:title]) {//之前是選中,現(xiàn)在取消選中[_selectArray removeObject:title];}else{//之前未選中,現(xiàn)在選中[_selectArray addObject:title];} //刷新被選中的cell
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}
#pragma mark? 設(shè)置每一行cell顯示的內(nèi)容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
? ? //定義循環(huán)利用標(biāo)識(shí)
? ? static NSString *identifier = @"profession";
? ? _cell = [tableView dequeueReusableCellWithIdentifier:identifier];
? ? //如果cell為空
? ? if (!_cell) {
? ? ? ? _cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier];
? ? }
? ? //取得cell模型數(shù)組? ? ? ? ?
? ? Profession *profession = _professionArray[indexPath.section];
? ? //取得cell模型數(shù)組中的title
? ? NSString *title = profession.trade[indexPath.row];
? ? //取得行業(yè)并設(shè)置給cell
? _cell.textLabel.text = title;? ? //判斷當(dāng)前title是否在已選中的數(shù)組中
? ? if ([_selectArray containsObject:title]) {
? ? ? ? //打鉤
? ? ? ? _cell.accessoryType = UITableViewCellAccessoryCheckmark;
? ? }else{
? ? ? ? //不打鉤
? ? ? ??_cell.accessoryType = UITableViewCellAccessoryNone;
? ? }
? ? return _cell;
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/hw140430/p/3749039.html
總結(jié)
以上是生活随笔為你收集整理的解决cell循环利用造成的重复勾选的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 短信转彩信限制页数
- 下一篇: tablefunc 行转列