tableView之cell的多选,全选以及左滑删除

本文介绍iOS应用中实现左滑删除和多选功能的方法,包括使用UITableView的代理方法实现左滑删除,以及通过自定义cell实现多选功能,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

刚好在公司做这一块的功能,现在来总结下,可能不是那么完善,以后慢慢再改进.

  • 左滑删除
  • 多选
  • 一种混合的写法(同时有侧滑和多选并自定义多选的图片)

左滑删除
#pragma mark 确定是否能编辑(删除和添加)
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

#pragma mark 编辑操作
// 只要实现了这个方法,cell默认会有左划效果. 默认的编辑样式是删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
        // 删除某些行的数据
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
    }else if (editingStyle == UITableViewCellEditingStyleInsert){...}
}

#pragma mark 删除按钮的文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"0.0";
}
多选

tableView有个代理方法: 当返回UITableViewCellEditingStyleDelete的时候是可以左滑删除, 神奇的是当返回UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert的时候 就变成了多选的样式.

#pragma mark 返回编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 返回多选样式
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

这里写图片描述


另一种写法

公司要求是cell能够左滑删除,能多选,能全选.

  • 1 利用xib来创建cell, 左边的小圆圈分为未选中和选中俩种状态(图片不同)
    这里写图片描述

  • 2 设置cell的属性 (titleLeftConstraint是titleLabel左边到小圆圈的约束)

@property (weak, nonatomic) IBOutlet UIButton *selBtn;
@property (nonatomic, assign) BOOL isSelected;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *titleLeftConstraint;
  • 3 控制器里设置一些属性,并重写isEdit的set方法
#define kData @"data"
#define kIsSelected @"selected"
NSMutableArray    *_dataArr ;
NSMutableArray    *_deleteArr; 
@property (nonatomic, assign) BOOL isEdit;
@property (nonatomic, assign) float titleLeftConstant;
- (void)setIsEdit:(BOOL)isEdit
{
    _isEdit = isEdit;

    if (isEdit) {
        self.titleLeftConstant = 10;
    } else {
        self.titleLeftConstant = -25;
    }

    [self.tableView reloadData];
}
  • 4 一些方法
// 对数据做下处理
// 服务器返回的数据为:(NSArray *)ResponseData
for (NSDictionary *item in ResponseData) {
        NSDictionary *dict = @{
                            kData : item,
                            kIsSelected : @(0)
                            };
    NSMutableDictionary *muDict = [NSMutableDictionary dictionaryWithDictionary:dict];
    [_dataArr addObject:muDict];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_isEdit) {
        return UITableViewCellEditingStyleNone;
    } else {
        return UITableViewCellEditingStyleDelete;
    }
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
     if (self.tableDatas.count) {
         [_dataArr removeObjectAtIndex:indexPath.row];
         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}
// 编辑
- (void)edit
{
    self.isEdit = YES;
}

// 取消编辑
- (void)cancelEdit
{
    self.isEdit = NO;
    if (_dataArr.count>0) {
        for (NSMutableDictionary *dict in _dataArr) {
            [dict setValue:@(0) forKey:kIsSelected];
        }
    }
}

// 全选
- (void)selectAll
{
    if (_dataArr.count>0) {
        for (NSMutableDictionary *dict in _dataArr) {
            [dict setValue:@(1) forKey:kIsSelected];
        }
    }
    [self.tableView reloadData];
}
// cell的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = _dataArr[indexPath.row];
    [dict setValue:@(![dict[kIsSelected] boolValue]) forKey:kIsSelected];
    if (_isEdit) { 
        cell.selectBtn.selected = [dict[kIsSelected] boolValue];
        if (cell.selectBtn.selected) {
             [_deleteArr addObject:indexPath];
        } else {
             [_deleteArr removeObject:indexPath];
        }
    }
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RemindCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RemindCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.selectBtn.selected = [_dataArr[indexPath.row][kIsSelected] boolValue];
    cell.selectBtn.hidden = !_isEdit;
    cell.titleLeftConstraint.constant = _titleLeftConstant;
    ......
    return cell;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值