UITableView多行删除

因为自己项目中用到UITableView多行删除,记录一下


//
//  ViewController.m
//  DeleteMoreCell
//
//  Created by Rambo on 16/4/11.
//  Copyright © 2016年 deleteCell. All rights reserved.
//

#import "ViewController.h"

@interface ViewController () 

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) NSMutableArray *dataArrayM;

@property (nonatomic,strong) NSMutableArray *deleteDataArrayM;

@property (nonatomic,strong) UIBarButtonItem *rightItem;

@property (nonatomic,strong) UIBarButtonItem *leftItem;

@property (nonatomic,strong) UIButton *deleteBtn;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.rightItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(editTableView:)];
    self.navigationItem.rightBarButtonItem = self.rightItem;
    
    self.leftItem = [[UIBarButtonItem alloc] initWithTitle:@"全选" style:UIBarButtonItemStylePlain target:self action:@selector(allSelectedTableViewCell:)];
    
    [self createTableView];
    
}

- (void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.editing = NO;
    self.tableView.tableFooterView = [UIView new];
    [self.view addSubview:self.tableView];
}

#pragma mark - UITableViewDelegate And UITabelViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArrayM.count;
}

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

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.dataArrayM removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
//    self.rightItem.title = @"完成";
    return @"删除";   //左滑删除的显示
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (!tableView.isEditing) {
        return UITableViewCellEditingStyleDelete;   //可以左滑删除
    }
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;   //可以多行删除
}

/** 选中cell */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView.isEditing) {
        [self.deleteDataArrayM addObject:self.dataArrayM[indexPath.row]];
    }
    
}

/** 取消选中cell */
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView.isEditing) {
        [self.deleteDataArrayM removeObject:self.dataArrayM[indexPath.row]];
    }
}

#pragma mark - Action
/** 编辑cell */
- (void)editTableView:(UIBarButtonItem*)barBtn{
    self.tableView.editing = NO;   //先取消编辑,只要是防止左滑删除冲突
    
    CGRect frame = self.tableView.frame;
    
    if ([barBtn.title isEqualToString:@"编辑"]) {//点击编辑
        self.tableView.editing = YES;
        self.tableView.allowsMultipleSelection = YES;
        
        frame.size.height -= self.deleteBtn.frame.size.height;
        self.tableView.frame = frame;
        
        self.deleteBtn.hidden = NO;
        //显示全选按钮
        self.navigationItem.leftBarButtonItem = self.leftItem;
        barBtn.title = @"完成";   //编辑/完成之间的转换
        
        return;
    }
        
    self.tableView.editing = NO;
    self.tableView.allowsMultipleSelection = NO;
    frame.size.height += self.deleteBtn.frame.size.height;
    self.tableView.frame = frame;
    self.deleteBtn.hidden = YES;
    self.navigationItem.leftBarButtonItem = nil;    //不显示全选按钮
    [self.deleteDataArrayM removeAllObjects];   //清除要删除多cell的数组
    barBtn.title = @"编辑";   //编辑/完成之间的转换
}

/** 全选cell */
- (void)allSelectedTableViewCell:(UIBarButtonItem*)barBtn{
    
    [self.deleteDataArrayM removeAllObjects];   //取消之前选中的cell
    
    [self.deleteDataArrayM addObjectsFromArray:self.dataArrayM];
    
    //把所以的cell都打勾
    for (int i = 0; i < self.dataArrayM.count; i ++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
    }
}

/** 删除cell */
- (void)deleteCell{
    [self.dataArrayM removeObjectsInArray:self.deleteDataArrayM];
    [self.tableView reloadData];
}

#pragma mark - get

- (NSMutableArray *)dataArrayM{
    if (!_dataArrayM) {
        _dataArrayM = [NSMutableArray array];
        for (int i = 0; i < 100; i++) {
            [_dataArrayM addObject:@(i)];
        }
    }
    return _dataArrayM;
}

- (NSMutableArray *)deleteDataArrayM{
    if (!_deleteDataArrayM) {
        _deleteDataArrayM = [NSMutableArray array];
    }
    return _deleteDataArrayM;
}

- (UIButton *)deleteBtn{
    if (!_deleteBtn) {
        _deleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame) - 44, self.view.frame.size.width, 44)];
        [_deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
        _deleteBtn.backgroundColor = [UIColor orangeColor];
        _deleteBtn.hidden = YES;
        [self.view addSubview:_deleteBtn];
        [_deleteBtn addTarget:self action:@selector(deleteCell) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteBtn;
}
@end

你可能感兴趣的:(UITableView多行删除)