iOS系统左滑删除

前言

整理一下UITableview的cell左滑删除的注意点,实现一个简单的左滑删除功能。整理的过程也是一个回归的过程,有时候一些功能很久没写就忘记了系统的实现方法。

iOS11之前的editActionsForRowAtIndexPath方法暂时不去适配,可以自行了解去适配iOS10版本

iOS11新增的系统侧滑方法

ios11新增的方法支持图片和文字侧滑样式, 默认的样式是图片在上,文字在下。滑动操作这里还有一个需要注意的是,当cell高度较小时,会只显示image,不显示title,当cell高度够大时,会同时显示image和title。

// Swipe actions
// These methods supersede -editActionsForRowAtIndexPath: if implemented
// return nil to get the default swipe actions
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvOS);
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvOS);
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) {
    UIContextualAction *action = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        if (self.dataArray.count > indexPath.row) {
            [self.dataArray removeObjectAtIndex:indexPath.row];
            [UIView performWithoutAnimation:^{
                [self.detailTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            }];
        }
        completionHandler(YES);
    }];
//    action.image = [UIImage imageNamed:@"tab_home"];
    action.backgroundColor = UIColorFromRGB(0xC9C6CC);
    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[action]];
    /// 控制是否可以左滑到头删除当前cell,默认为YES
//    config.performsFirstActionWithFullSwipe = NO;
    self.editingIndexPath = indexPath;
    [self.view setNeedsLayout];
    return config;
}

不同系统版本对左滑样式的处理

/// 设置左滑菜单按钮的样式
- (void)setupSlideBtn {
    if (@available(iOS 13.0, *)) {
        for (UIView *subView in self.detailTableView.subviews) {
            if ([subView isKindOfClass:NSClassFromString(@"_UITableViewCellSwipeContainerView")] && [subView.subviews count] >= 1) {
                UIView *remarkContentView = subView.subviews.firstObject;
                [self setupRowActionViewInit: remarkContentView];
            }
        }
        return;
    }
    if (@available(iOS 11.0, *)) {
        for (UIView *subView in self.detailTableView.subviews) {
            if ([subView isKindOfClass:NSClassFromString(@"UISwipeActionPullView")] && [subView.subviews count] >= 1) {
                UIView *remarkContentView = subView;
                remarkContentView.backgroundColor = [UIColor clearColor];
                [self setupRowActionViewInit: remarkContentView];
            }
        }
        return;
    }
    // iOS11 以下的版本
    UITableViewCell *cell = [self.detailTableView cellForRowAtIndexPath:self.editingIndexPath];
    for (UIView *subView in cell.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")] && [subView.subviews count] >= 1) {
            UIView *remarkContentView = subView;
            [self setupRowActionViewInit:remarkContentView];
        }
    }
}

自定义cell高度时左滑样式处理

左滑删除的系统样式默认是cell的高度,当在cell显示的View和cell本身高度不等高时候需要处理

/// 系统默认的侧滑删除高度是和cell等高的,修改样式
- (void)setupRowActionViewInit:(UIView *)rowActionView {
    CGRect frame = rowActionView.frame;
    if (self.editingIndexPath == nil) {
        if (frame.origin.y > 0) {
            frame.origin.y = 0;
            frame.size.height += 15*KYY;
        }
    } else {
        if (frame.origin.y == 0) {
            frame.origin.y += 15*KYY;
            frame.size.height -= 15*KYY;
        }
    }
    rowActionView.frame = frame;
    UIButton *button = rowActionView.subviews.firstObject;
    [button setTitle:@"删除" forState:UIControlStateNormal];
//    [button setImage:[UIImage imageNamed:@"tab_home"] forState:UIControlStateNormal];
    button.titleLabel.font = UIDEFAULTFONTSIZE(16);
}

demo链接

你可能感兴趣的:(iOS系统左滑删除)