iOS使用NSMutableSet记录cell控件选中状态避免cell重用问题
2016-10-31 10:12
661 查看
我在tableView上面有个button,当我选中button的时候,上滑页面发现选中的状态没有了,但是数组里面添加的button tag值还在(类似于购物车那种方式)很是蛋疼,幸亏还有NSMutableSet来拯救我们啊 哈哈 废话不多说,直接看代码
1、首先我们定义一个NSMutableSet的属性
//用来记录选中的状态
@property (nonatomic, strong)NSMutableSet *selectdeSet;
2、初始化
3、在需要记录状态的地方记录选中状态,比如现在记录每行cell的tag值
-(UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
}
-(void)chooseAction:(UIButton *)sender
{
}
到此,使用NSMutableSet记录选中状态的方法结束,我们只需要注意的是,当你给数组添加对象的时候,记得给NSMutableSet添加对象,同样当你删除掉数组里面对象的时候记得删除点NSMutableSet中的对象
1、首先我们定义一个NSMutableSet的属性
//用来记录选中的状态
@property (nonatomic, strong)NSMutableSet *selectdeSet;
2、初始化
self.selectdeSet = [NSMutableSet set];
3、在需要记录状态的地方记录选中状态,比如现在记录每行cell的tag值
-(UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"cell"; XiaLaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[XiaLaTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } XiaLaModel *model = self.arrModel[indexPath.row]; cell.xialaModel = model; cell.thirdLabel.tag = 100 + indexPath.row; [cell.thirdLabel addTarget:self action:@selector(chooseAction:) forControlEvents:(UIControlEventTouchUpInside)]; //这个是NSMutableSet 判断这个集合中是否存在传入的对象,返回Bool值,如果是则此cell为选中状态 否则为非选中状态 if ([self.selectdeSet containsObject:[NSString stringWithFormat:@"%ld", 100 + indexPath.row]]) { cell.thirdLabel.selected = YES; [cell.thirdLabel setTitleColor:[UIColor whiteColor] forState:(UIControlStateSelected)]; } return cell;
}
-(void)chooseAction:(UIButton *)sender
{
if (sender.selected == NO) { sender.selected = YES; [sender setTitleColor:[UIColor whiteColor] forState:(UIControlStateSelected)]; //向数组中添加选中的对象 [self.dataArray addObject:[NSString stringWithFormat:@"%ld", sender.tag]]; //向NSMutableSet动态添加选中的对象 [self.selectdeSet addObject:[NSString stringWithFormat:@"%ld", sender.tag]]; } else { sender.selected = NO; for (int i = 0; i < self.dataArray.count; i++) { if ([[self.dataArray objectAtIndex:i] isEqualToString:[NSString stringWithFormat:@"%ld",(long)sender.tag]]) { // 删除数组中选中的对象 [self.dataArray removeObjectAtIndex:i]; //删除NSMutableSet中选择的对象 [self.selectdeSet removeObject:[NSString stringWithFormat:@"%ld", sender.tag]]; } } }
}
到此,使用NSMutableSet记录选中状态的方法结束,我们只需要注意的是,当你给数组添加对象的时候,记得给NSMutableSet添加对象,同样当你删除掉数组里面对象的时候记得删除点NSMutableSet中的对象
相关文章推荐
- iOS tableView 的 cell上其它控件重用(利用view的tag属性巧妙实现重用,从xib中加在cell重载问题)
- IOS开发之因Cell重用导致控件的状态级联改变
- iOS UITableViewCell重用机制避免重复显示问题
- iOS开发学习专题-基础知识(二)NSArray数组NSMutableArray可变数组NSSet集合NSMutableSet可变集合详细使用方式
- iOS 关于UICollectionView选中状态,当使用reloadData方法时出现数据的问题?
- iOS-使用NSMutableArray时添加对象报错问题
- iOS如何在容器类(如NSMutableSet)中使用弱引用(weak reference)
- 【iOS开发】NSMutableArray使用中忽视的问题
- ios cell 控件状态被重用,教你三步跳出坑(cell里面控件状态版)
- iOS UITableViewCell使用gif图片的卡顿和重用问题
- iOS开发OC基础:OC集合NSSet、NSMutableSet以及NSCountedSet的基本方法的使用
- 关于QTableWidget使用setCellWidget嵌入控件错位的问题
- iOS 使用NSArray(NSMutableArray)接受plist文件数据
- 集合:NSArray、NSMutableArray、NSDictionary、NSMutableDictionary、NSSet、NSMutableSet使用
- NSet 和 NSMutableSet 的简单使用
- ios之清除cell缓存,解决cell的重用问题。
- 【Mac OS X开发】NSTextFieldCell选中和正常状态的字体段落样式设置
- ios开发 UITableCell 可重用问题
- [ios] NSSet,NSMutableSet[zhuan]
- 一个JS图片放大镜,实现动态记录的图片放大,避免内存泄露,解决IE6下无法遮盖select控件问题