tableView-删除、插入、多选删除

  • 左滑删除
//声明
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;//数据源

//懒加载
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

//添加tableView
- (void)createUI
{
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.and.trailing.equalTo(@0);
        make.top.equalTo(self.mas_topLayoutGuide);
        make.bottom.equalTo(self.mas_bottomLayoutGuide);
    }];
}

//数据
- (void)loadData
{
    for (int i = 0; i < 20; i ++) {
        [self.dataArray addObject:[NSString stringWithFormat:@"%d", i]];
    }
}

//数据源代理 -展示数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

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

//删除代理
//是否可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
        [self.dataArray removeObjectAtIndex:indexPath.row];//删除数据源中相应数据
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

//修改删除操作title -默认 "删除"(Delete) -跟随系统语言
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"左滑删除";
}

复制代码

  • 编辑状态下删除

与左滑返回区别: 设置编辑状态为YES

self.tableView.editing = !self.tableView.editing;

//编辑风格为删除
if (self.editingStyle == CCTableViewCellEditingStyleDelete) {
            return UITableViewCellEditingStyleDelete;
        }
复制代码

点击编辑,出现减号,再点击减号 出现左滑返回按钮 点击左滑返回按钮 删除数据

  • 编辑状态下插入
//编辑风格为插入
if (self.editingStyle == CCTableViewCellEditingStyleInsert) {
            return UITableViewCellEditingStyleInsert;
        }

//插入操作
[self.dataArray insertObject:@"cc" atIndex:indexPath.row];//插入数据
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
复制代码

  • 编辑风格为删除和插入共存
if (self.editingStyle == (CCTableViewCellEditingStyleDelete | CCTableViewCellEditingStyleInsert)) {
            return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
        }

//声明
@property (nonatomic, strong) NSMutableDictionary *deleteDic;//记录多选删除数据
@property (nonatomic, strong) UIBarButtonItem *deleteBtn;

//显示删除按钮
if (self.editingStyle == (CCTableViewCellEditingStyleInsert | CCTableViewCellEditingStyleDelete)) {
        
        [self.deleteDic removeAllObjects];
        
        self.deleteBtn = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(deleteBtnAction)];
        NSMutableArray *Items = [[NSMutableArray alloc]initWithObjects:self.deleteBtn, nil];
        
        if (self.tableView.editing) {
            [self.navigationController setToolbarHidden:NO animated:YES];
        }else {
            [self.navigationController setToolbarHidden:YES animated:YES];
        }
        
        [self setToolbarItems:Items];
        self.deleteBtn.enabled = NO;
    }

//点击cell选择删除行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //indexPath为key 对应数据为值
    [self.deleteDic setObject:_dataArray[indexPath.row] forKey:indexPath];
    if (self.deleteDic.count == 0) {
        self.deleteBtn.enabled = NO;
    } else {
        self.deleteBtn.enabled = YES;
    }
}

//取消行
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.deleteDic removeObjectForKey:indexPath];
    if (self.deleteDic.count == 0) {
        self.deleteBtn.enabled = NO;
    } else {
        self.deleteBtn.enabled = YES;
    }
}

//删除操作
//多选删除操作
    [_dataArray removeObjectsInArray:[self.deleteDic allValues]];
    [self.tableView deleteRowsAtIndexPaths:[self.deleteDic allKeys] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.deleteDic removeAllObjects];
    [self.tableView setEditing:!self.tableView.editing animated:YES];
    [self.navigationController setToolbarHidden:YES animated:YES];
复制代码

左滑删除很多应用中都会此功能,而其他的系统自带功能或许并不一定符合实际需求;关于多选删除,过几天会分享一个小例子

代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值