iOS tableView编辑删除(单选 多选)

viewDidload 添加
for (int i = 0; i <_reouseArray.count ; i++) {
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[dictionary setValue:@"NO" forKey:[NSString stringWithFormat:@"%d", i]];
[_contacts addObject:dictionary];
}
cellForRow中进行赋值
    NSMutableDictionary *dictionary = [_contacts objectAtIndex:[indexPath row]];
    if ([[dictionary objectForKey:keyForChecked] isEqualToString:@"NO"]) {
        [dictionary setObject:@"NO" forKey:keyForChecked];
        [_showMediaCell setChecked:NO];
        
    }else {
        [dic_material setObject:@"YES" forKey:keyForChecked];
        [_showMediaCell setChecked:YES];
        
    }

#define keyForChecked [NSString stringWithFormat:@"%ld", (long)indexPath.row]

<pre name="code" class="objc">didSelect方法获取单个点击/取消操作
 
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    NewShowCollectionViewCell *cell = (NewShowCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
    dic_material = [_contacts objectAtIndex:[indexPath row]];

    if ([[dic_material objectForKey:keyForChecked] isEqualToString:@"NO"] ) {// && [_editState isEqualToString:@"on"]

        [dic_material setObject:@"YES" forKey:keyForChecked];
        [cell setChecked:YES];
        id addObject = [_reouseArray objectAtIndex:[indexPath row]];
        [self.removeList addObject:addObject];

    }else{
 
        [dic_material setObject:@"NO" forKey:keyForChecked];
        [cell setChecked:NO];
         id removeObject = [_reouseArray objectAtIndex:[indexPath row]];
        [self.removeList removeObject:removeObject];
        
    }
 
}


// 全选操作
- (void)editAllTheDeleteItems {
    
        if (is_editAll) {
    
            for (int i = 0; i < self.reouseArray.count; i ++) {
 
                NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
                NewShowCollectionViewCell *cell = (NewShowCollectionViewCell*)[self.collectionView cellForItemAtIndexPath:indexPath];
                NSUInteger row = [indexPath row];
                NSMutableDictionary *dictionary = [_contacts objectAtIndex:row];
                [dictionary setObject:@"YES" forKey:keyForChecked];
                [cell setChecked:YES];
                [self.removeList addObject:_reouseArray[i]];
                is_editAll = NO;
                [_editAllBtn setImage:[UIImage imageNamed:@"全选2"] forState:(UIControlStateNormal)];
             }
    
        }else{
    
            for (int i = 0; i < self.reouseArray.count; i ++) {
                
                NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
                NewShowCollectionViewCell *cell = (NewShowCollectionViewCell*)[self.collectionView cellForItemAtIndexPath:indexPath];
                NSUInteger row = [indexPath row];
                NSMutableDictionary *dictionary = [_contacts objectAtIndex:row];
                [dictionary setObject:@"NO" forKey:keyForChecked];
                [cell setChecked:NO];
                [self.removeList removeObject:_reouseArray[i]];
                is_editAll = YES;
                [_editAllBtn setImage:[UIImage imageNamed:@"全选1"] forState:(UIControlStateNormal)];
 
            }
        }

}
自定义cell中
<pre name="code" class="objc">- (void)setChecked:(BOOL)checked{
    if (checked)
    {
        [_selectBtn setImage:[UIImage imageNamed:@"选择2"] forState:(UIControlStateNormal)];
    }
    else
    {
        [_selectBtn setImage:[UIImage imageNamed:@"选择1"] forState:(UIControlStateNormal)];
    }
    _checkedForSelect = checked;
}


 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值