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多选

你可能感兴趣的:(tableView多选 & collectionView多选)