iOS UITableView自定義左滑編輯按鈕樣式

自定義左滑編輯按鈕樣式,具體代碼編寫邏輯我就不寫了,網(wǎng)上很多文章都有介紹,我就貼下適配各個版本系統(tǒng)下圖層代碼和注意事項了。

一、觸發(fā)修改樣式的方法

1、控制器中使用viewDidLayoutSubviews
2、自定義TableView中使用layoutSubviews

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    if (self.editingIndexPath) {
        [self csc_configTableViewSwipeButtons];
    }
}

二、圖層代碼

  - (void)csc_configTableViewSwipeButtons {
    //XCode 10.2.1
    //iOS 13層級: UITableView -> UISwipeActionPullView
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"13.0")) {
        for (UIView *subview in self.tableView.subviews) {
            if ([subview isKindOfClass:NSClassFromString(@"_UITableViewCellSwipeContainerView")]) {
                for (UIView *subView_sub in subview.subviews) {
                    if ([subView_sub isKindOfClass:NSClassFromString(@"UISwipeActionPullView")] &&
                        [subView_sub.subviews count] >= 1) {
                        subView_sub.backgroundColor = [UIColor grayBackgoundColor];
                        // 和iOS 10的按鈕順序相反
                        UIButton *deleteButton = subView_sub.subviews[0];
                        [self csc_configTableViewDeleteButton:deleteButton];
                        break;
                    }
                }
            }
        }
    }
    else if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"11.0")) {
        // iOS 11-12層級: UITableView -> UISwipeActionPullView
        for (UIView *subview in self.tableView.subviews) {
            if ([subview isKindOfClass:NSClassFromString(@"UISwipeActionPullView")] &&
                [subview.subviews count] >= 1) {
                subview.backgroundColor = [UIColor grayBackgoundColor];
                // 和iOS 10的按鈕順序相反
                UIButton *deleteButton = subview.subviews[0];
                [self csc_configTableViewDeleteButton:deleteButton];
                break;
            }
        }
    }
    else {
        // iOS 8-10層級: UITableView -> UITableViewCell -> UITableViewCellDeleteConfirmationView
        YDCouponTableViewCell *tableCell = [self.tableView cellForRowAtIndexPath:self.editingIndexPath];
        for (UIView *subview in tableCell.subviews) {
            if ([subview isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")] &&
                [subview.subviews count] >= 1) {
                subview.backgroundColor = [UIColor grayBackgoundColor];
                UIButton *deleteButton = subview.subviews[0];
                [self csc_configTableViewDeleteButton:deleteButton];
                break;
            }
        }
    }
}

方法在里寫的兩個方法中調(diào)用。
SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO是我寫的宏判斷系統(tǒng)的
csc_configTableViewDeleteButton寫配置樣式就好。

三、UITableVIew Delegate方法

在iOS13以前,只需要這樣寫

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    self.editingIndexPath = indexPath;
    //這里會觸發(fā)viewDidLayoutSubviews方法,從而執(zhí)行修改樣式代碼
    [self.view setNeedsLayout];
}

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(nullable NSIndexPath *)indexPath {
    self.editingIndexPath = nil;
}

在iOS13中,同一個cell多次左右滑動只會調(diào)用一次willBeginEditingRowAtIndexPath,就出現(xiàn)了第一次樣式是對的,后面的就沒了。
于是我加了下面一段代碼

- (NSArray*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    //系統(tǒng)大于13
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"13.0")) {
        self.editingIndexPath = indexPath;
        [self.view setNeedsLayout];
    }
    
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [tableView setEditing:NO animated:YES];
        
    }];
    return @[deleteAction];
}

目前沒什么問題,就先這樣吧!
有時間了再研究研究。

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

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

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