iOS系統(tǒng)左滑刪除

前言

整理一下UITableview的cell左滑刪除的注意點,實現(xiàn)一個簡單的左滑刪除功能。整理的過程也是一個回歸的過程,有時候一些功能很久沒寫就忘記了系統(tǒng)的實現(xiàn)方法。

iOS11之前的editActionsForRowAtIndexPath方法暫時不去適配,可以自行了解去適配iOS10版本

iOS11新增的系統(tǒng)側滑方法

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;
}

不同系統(tǒng)版本對左滑樣式的處理

/// 設置左滑菜單按鈕的樣式
- (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高度時左滑樣式處理

左滑刪除的系統(tǒng)樣式默認是cell的高度,當在cell顯示的View和cell本身高度不等高時候需要處理

/// 系統(tǒng)默認的側滑刪除高度是和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鏈接

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容