iOS 自定义左滑/侧滑删除,适配 iOS13 以上版本

5,348

众所周知,在 iOS 里 UITableView 的左滑/侧滑删除的功能还是挺美观的,最近项目的需求里就用到了这个样式

需求图片
当时接到这个需求的时候也在网上查找了很多资料,非常感谢这篇文章带给我的灵感

iOS cell自定义左滑/侧滑删除(支持iOS11)

iOS13 以上的系统左滑控件的层级以及类名都发生了变化,以前的方法明显不够用了,通过不断的断点分析,类名输出后终于正确的适配上了 iOS13,特在此记录一下

1. 定义左滑出来的控件方法

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 这里的标题我使用的 4 个空格进行占位
    UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"    " handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        // 点击删除按钮需要执行的方法
        
        [tableView setEditing:NO animated:YES];
    }];
    
    // 修改背景颜色
    action.backgroundColor = hexColor(0xEB1163);
    
    return @[action];
}

2. 监听即将开启编辑状态的事件

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    // 在 iOS11 以下系统,因为方法线程问题,需要放到主线程执行, 不然没有效果
    dispatch_async(dispatch_get_main_queue(), ^{
        [self setupSlideBtnWithEditingIndexPath:indexPath];
    });
}

3.设置左滑出来的按钮样式

//MARK: 设置左滑按钮的样式
- (void)setupSlideBtnWithEditingIndexPath:(NSIndexPath *)editingIndexPath {

    // 判断系统是否是 iOS13 及以上版本
    if (@available(iOS 13.0, *)) {
        for (UIView *subView in self.tableView.subviews) {
            if ([subView isKindOfClass:NSClassFromString(@"_UITableViewCellSwipeContainerView")] && [subView.subviews count] >= 1) {
                // 修改图片
                UIView *remarkContentView = subView.subviews.firstObject;
                [self setupRowActionView:remarkContentView];
            }
        }
        return;
    }
    
    // 判断系统是否是 iOS11 及以上版本
    if (@available(iOS 11.0, *)) {
        for (UIView *subView in self.tableView.subviews) {
            if ([subView isKindOfClass:NSClassFromString(@"UISwipeActionPullView")] && [subView.subviews count] >= 1) {
                // 修改图片
                UIView *remarkContentView = subView;
                [self setupRowActionView:remarkContentView];
            }
        }
        return;
    }
    
    // iOS11 以下的版本
    MTMineCollectionTableViewCell *cell = [self.tableView cellForRowAtIndexPath:editingIndexPath];
    for (UIView *subView in cell.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")] && [subView.subviews count] >= 1) {
            // 修改图片
            UIView *remarkContentView = subView;
            [self setupRowActionView:remarkContentView];
        }
    }
}

- (void)setupRowActionView:(UIView *)rowActionView {
    // 切割圆角
    [rowActionView cl_setCornerAllRadiusWithRadiu:20];
    // 改变父 View 的frame,这句话是因为我在 contentView 里加了另一个 View,为了使划出的按钮能与其达到同一高度
    CGRect frame = rowActionView.frame;
    frame.origin.y += kFitRealValue(7);
    frame.size.height -= kFitRealValue(13);
    rowActionView.frame = frame;
    // 拿到按钮,设置图片
    UIButton *button = rowActionView.subviews.firstObject;
    [button setImage:kImageName(@"delete_col") forState:UIControlStateNormal];
    [button setTitle:@"" forState:UIControlStateNormal];
}

到这里我们就已经实现我们需要的效果了,但是在 iOS13 以上,有时候快速左滑/侧滑时并不能执行

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath;

方法,所以会自定义不了按钮,但是轻轻滑动却能执行,目前不清楚是什么原因,希望知道的大佬能告诉一下,谢谢。

如果这篇文章对您有帮助,希望可以动下小手点个赞,谢谢。