tableviewcell 系统工具删除:左滑删除,选中多个删除

在编辑tableview的时候,我们有时候会需要编辑对一些cell进行删除(增加),在这个时候,我们最好用系统的方法来进行增删的操作

//
//  text1Controller.m
//  text
//
//  Created by 123 on 16/5/5.
//  Copyright © 2016年 yipinbaike. All rights reserved.
//

#import "text1Controller.h"

@interface text1Controller ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView * _mainTableView;
    UITableViewCellEditingStyle _editStyle;
    NSMutableArray * _dataArray;
    NSMutableArray * _delArray;
}
@end

@implementation text1Controller

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //数据源
    _dataArray = [[NSMutableArray alloc]init];
    for (int i = 0; i<20; i++) {
        NSString *text = [NSString stringWithFormat:@"fstext-%i", i];
        [_dataArray addObject:text];
    }
    //删除数据源
    _delArray = [[NSMutableArray alloc]init];
    [self createTableView];
}


- (void)createTableView{
    UIButton * btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width/2, 50)];
    [self.view addSubview:btn1];
    [btn1 addTarget:self action:@selector(editing) forControlEvents:UIControlEventTouchUpInside];
    btn1.backgroundColor = [UIColor whiteColor];
    [btn1 setTitle:@"编辑" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    UIButton * btn2 = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2, 0, self.view.frame.size.width/2, 50)];
    [self.view addSubview:btn2];
    [btn2 addTarget:self action:@selector(delete) forControlEvents:UIControlEventTouchUpInside];
    btn2.backgroundColor = [UIColor whiteColor];
    [btn2 setTitle:@"删除" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height-50)];
    [self.view addSubview:_mainTableView];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    _editStyle =UITableViewCellEditingStyleDelete;
}

- (void)delete{
    if (_mainTableView.isEditing) {
        for (id model in _delArray) {
            [_dataArray removeObject:model];
        }
        [_delArray removeAllObjects];
        [_mainTableView reloadData];
        _mainTableView.editing = NO;
        _editStyle = UITableViewCellEditingStyleDelete;
    }
}
- (void)editing{
    _editStyle = UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
    _mainTableView.editing = !_mainTableView.isEditing;
    if (!_mainTableView.editing) {
        _editStyle = UITableViewCellEditingStyleDelete;
        [_delArray removeAllObjects];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = _dataArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return _editStyle;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.isEditing) {
        [_delArray addObject:_dataArray[indexPath.row]];
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.isEditing) {
        [_delArray removeObject:_dataArray[indexPath.row]];
    }
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle==UITableViewCellEditingStyleDelete) {
        [_dataArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}
//设置删除按钮现实中文
- (NSString*)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
原文地址:https://www.cnblogs.com/fusheng-it/p/5462003.html