iOS使用NSMutableSet记录cell控件选中状态避免cell重用问题

假如现在tableViewCell上有一个button,当我选中button的时候,上下滑动,发现button选中的状态消失了,但是数组里面添加的button.tag值还在……
如何避免这个问题呢,我们使用NSMutableSet来解决这个问题......
1、首先,我们定义一个NSMutableSet的属性

@property (nonatomic, strong)NSMutableSet *selectdeSet; //记录选中状态
@property (nonatomic, strong)NSMutableArray *dataArray;//数组中添加选中时候的tag值 

2、viewdidload里面初始化

self.selectdeSet = [NSMutableSet set];
self.dataArray = [NSMutableArray array];

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中的对象

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 添加对Interface Builder的支持 如果你在Interface Builder中看rating控件,你...
    raingu24阅读 4,962评论 0 1
  • 1.badgeVaule气泡提示 2.git终端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夹内容...
    i得深刻方得S阅读 10,279评论 1 9
  • 2017.02.22 可以练习,每当这个时候,脑袋就犯困,我这脑袋真是神奇呀,一说让你做事情,你就犯困,你可不要太...
    Carden阅读 5,244评论 0 1
  • 上帝许诺让我做个旁观者 见证这份执着的爱恋 见证校园宁静的美丽 见证真爱萌发的季节 在最晴朗的天空许诺 诺,做你的...
    百哩系阅读 1,319评论 0 1
  • 久违的阳光来了,窗帘没有拉开,阳光已经迫不及待的洒在了床上,被单上的牡丹花特别美。 这两天在看一个真人秀的节目,《...
    元宵Mickey阅读 1,540评论 0 0

友情链接更多精彩内容