iOS 編輯tableView的cell多選(全選)刪除功能

本期帶來 tableview 的編輯模式,多選刪除、全選刪除,統(tǒng)計選中刪除數(shù)功能。

![Snip20160814_2.png](http://upload-images.jianshu.io/upload_images/1737720-0e07c6f843f478af.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

實現(xiàn)下面代理方法可以調(diào)用系統(tǒng)的選擇 cell 左邊的藍色選中圖標,當然必須是tableView.editing = YES;

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;   
}

刪除 cell 用一個中介數(shù)組變量來進行統(tǒng)計要刪除的 cell
選擇 cell 用方法
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionBottom];
注意要用UITableViewScrollPositionBottom,這樣才能從頭選到尾,不會出現(xiàn)只選擇屏幕顯示的區(qū)域

具體代碼


#import "ViewController.h"
#import "Masonry.h"
#import "UIButton+Extention.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) UITableView *tableView;
@property (weak, nonatomic) UIButton *selectedBtn;
@property (weak, nonatomic) UIButton *deleteBtn;
@property (weak, nonatomic) UIImageView *editView;
@property (strong, nonatomic) NSMutableArray *dataArr;
@property (strong, nonatomic) NSMutableArray *deleteArr;
@property (assign, nonatomic) NSInteger deleteNum;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.deleteArr = [[NSMutableArray alloc]init];
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 70)];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    tableView.delegate = self;
    tableView.dataSource = self;
    
    tableView.editing = YES;
    
    //    編輯區(qū)域
    UIImageView *editView = [[UIImageView alloc]init];
    [self.view addSubview:editView];
    self.editView = editView;
    
    editView.userInteractionEnabled = YES;
    //    editView.hidden = YES;
    editView.image = [UIImage imageNamed:@"MyLivingBtnBackground.png"];
    
    
    [editView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.equalTo(@65);
    }];
    
    
    
    //    全選
    UIButton *selectedBtn = [UIButton buttonWithImage:@"AllSelectedBtn" title:@"全選" target:self action:@selector(selectedBtnClick)];
    [editView addSubview:selectedBtn];
    self.selectedBtn = selectedBtn;
    
    [selectedBtn setTitle:@"取消全選" forState:UIControlStateSelected];
    
    [selectedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(140, 45));
        make.centerY.equalTo(editView);
        make.centerX.equalTo(@-80);
    }];
    
    //    刪除
    UIButton *deleteBtn = [UIButton buttonWithImage:@"delete_btn" title:@"刪除(0)" target:self action:@selector(deleteBtnClick)];
    [editView addSubview:deleteBtn];
    self.deleteBtn = deleteBtn;
    
    [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(140, 45));
        make.centerY.equalTo(editView);
        make.centerX.equalTo(@80);
    }];
}
#pragma mark - 全選按鈕被點擊
- (void)selectedBtnClick {
    if (!self.selectedBtn.selected) {
        self.selectedBtn.selected = YES;
        
        for (int i = 0; i < self.dataArr.count; i++) {
            
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionBottom];
        }
        [self.deleteArr addObjectsFromArray:self.dataArr];
        self.deleteNum = self.dataArr.count;
        [self.deleteBtn setTitle:[NSString stringWithFormat:@"刪除(%lu)",self.deleteNum] forState:UIControlStateNormal];
    }else{
        self.selectedBtn.selected = NO;
        [self.deleteArr removeAllObjects];
        for (int i = 0; i < self.dataArr.count; i++) {
            
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
            //            cell.selected = NO;
        }
        self.deleteNum = 0;
        [self.deleteBtn setTitle:[NSString stringWithFormat:@"刪除(%lu)",self.deleteNum] forState:UIControlStateNormal];
    }
}

#pragma mark - 刪除按鈕
- (void)deleteBtnClick {
    if (self.tableView.editing) {
        //刪除

        [self.dataArr removeObjectsInArray:self.deleteArr];
        [self.tableView reloadData];
        
        self.deleteNum = 0;
        [self.deleteBtn setTitle:[NSString stringWithFormat:@"刪除(%lu)",self.deleteNum] forState:UIControlStateNormal];
        
        self.selectedBtn.selected = NO;
        //            你的網(wǎng)絡請求
        
        
    }
}


#pragma mark - DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cellID";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"%@",self.dataArr[indexPath.row]];

    return cell;
}

#pragma mark - tableViewDelegate

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.deleteArr addObject:[self.dataArr objectAtIndex:indexPath.row]];
    self.deleteNum += 1;
    [self.deleteBtn setTitle:[NSString stringWithFormat:@"刪除(%lu)",self.deleteNum] forState:UIControlStateNormal];
    
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.deleteArr removeObject:[self.dataArr objectAtIndex:indexPath.row]];
    self.deleteNum -= 1;
    [self.deleteBtn setTitle:[NSString stringWithFormat:@"刪除(%lu)",self.deleteNum] forState:UIControlStateNormal];
}

#pragma mark - 懶加載
- (NSMutableArray *)dataArr {
    if (_dataArr == nil) {
        _dataArr = [[NSMutableArray alloc]init];
        for (int i = 0; i < 100; ++i) {
            [_dataArr addObject:@(i)];
        }
    }
    return _dataArr;
}


@end

如果不想用系統(tǒng)自帶的藍色圓圈選中樣式,想要修改為自定義的選中樣式,可以看我的這篇博文http://www.itdecent.cn/p/678da944f6fb

gitHub 地址:https://github.com/D-james/MultipleSelectCell

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容