tableView多选 & collectionView多选

1 tableView多选

method 1
self.tableView.allowsMultipleSelection = YES;// 允许多选


选中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.textLabel.textColor = [UIColor lightGrayColor];
    cell.textLabel.text = [NSString stringWithFormat:@"已举报 %@",self.dataArray[indexPath.row]];
    [self.selectAry addObject:self.dataArray[indexPath.row]];
}


// 取消选中
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.text = [NSString stringWithFormat:@"%@",self.dataArray[indexPath.row]];
    [self.selectAry removeObject:self.dataArray[indexPath.row]];
}
多选1.gif
method 2
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { //如果为选中状态
        cell.accessoryType = UITableViewCellAccessoryNone; //切换为未选中
        cell.textLabel.textColor = [UIColor blackColor];
        [self.selectAry removeObject:self.dataArray[indexPath.row]];
        cell.textLabel.text = [NSString stringWithFormat:@"%@",self.dataArray[indexPath.row]];
    }else { //未选中
        cell.accessoryType = UITableViewCellAccessoryCheckmark; //切换为选中
        cell.textLabel.textColor = [UIColor lightGrayColor];
        cell.textLabel.text = [NSString stringWithFormat:@"已举报 %@",self.dataArray[indexPath.row]];
        [self.selectAry addObject:self.dataArray[indexPath.row]];
     }
}
多选2

2 collectionView多选

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeAlertCCell * cell = (HomeAlertCCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.titleLab.layer.borderColor = kRedColor.CGColor;
    cell.titleLab.textColor = kRedColor;
    self.selectCount += 1;
    self.reaCount.text = [NSString stringWithFormat:@"已选%d个理由",self.selectCount];
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeAlertCCell * cell = (HomeAlertCCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.titleLab.layer.borderColor = Line_Color.CGColor;
    cell.titleLab.textColor = TextDark_Color;
    if (self.selectCount > 0) {
        self.selectCount -= 1;
        if (self.selectCount == 0) {
            self.reaCount.text = [NSString stringWithFormat:@"可选理由,精准屏蔽"];
        } else{
           self.reaCount.text = [NSString stringWithFormat:@"已选%d个理由",self.selectCount];
        }
    } else {
        self.reaCount.text =[NSString stringWithFormat:@"可选理由,精准屏蔽"];
    }
}
collection多选
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,252评论 4 61
  • 电影开篇西西里蓝色的海,伴随着海风镜头慢慢拉远,讲述一个西西里少年的成长故事。 天堂电影院这个译名与电影内容无缝匹...
    zpygo1阅读 553评论 0 1
  • 今天的内容是:一个人的格局被放大了,就永远回不到原来的小地方。 面对内心的恐惧,面对自己的关系,面对未知的自己。 ...
    长期主义者庆福阅读 241评论 0 0
  • 爱上声音,爱上阅读! 默然读书会,用声音伴你阅读! 每天跟读练习普通话,培养良好语感; 从书中汲取营养,丰富阅历,...
    AAA兰兰Kane阅读 184评论 0 0
  • 办公室里有老鼠,早晨过去的时候,看到沙琪玛掉在地上。川哥,说了一句他们是绝对的使命导向,我看也未必。假如比人类更高...
    暗夜阅读 183评论 0 0