在項(xiàng)目開發(fā)中我們往往會用到UITableView自帶的左滑刪除功能驻襟,但有時候系統(tǒng)現(xiàn)有的功能并不能完全滿足我們的開發(fā)需求,這樣就需要我們在其現(xiàn)有的功能基礎(chǔ)上自定義我們所需要的功能了抠藕。下圖是在項(xiàng)目中自定義的按鈕(只是修改了按鈕的frame而已)克婶。
然后我就總結(jié)了一下根據(jù)不同的需求自定義不同的按鈕获洲。
一磨取、系統(tǒng)默認(rèn)左滑刪除按鈕
如果你對左滑刪除按鈕的要求不高元潘,僅僅只是實(shí)現(xiàn)UITableView上cell的左滑刪除功能畔乙,那在UITableView的代理方法中添加以下兩種方法便可實(shí)現(xiàn)需求:
//使用系統(tǒng)默認(rèn)的刪除按鈕
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete){
}
}
//自定義系統(tǒng)默認(rèn)的刪除按鈕文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"自定義按鈕”;
}
效果如下所示:
雖然這樣能基本實(shí)現(xiàn)功能,但是我們發(fā)現(xiàn)右邊的按鈕和左邊的黃色區(qū)域的高度并不一樣翩概。這是因?yàn)橛疫叞粹o是和UITableViewCell的高度一致牲距,而左邊的黃色區(qū)域只是一張圖片而已袖订,其高度設(shè)置和UITableViewCell的高度并不一致,才會導(dǎo)致這樣的布局出現(xiàn)嗅虏。如果我們想要刪除按鈕和左邊圖片一樣的高度洛姑,那我們就需要自定義刪除按鈕的高度了。
二皮服、自定義左滑刪除按鈕
如果我們想要實(shí)現(xiàn)不止一個自定義按鈕的功能楞艾,那我們就需要在UITableView代理方法- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {}
中添加我們所需要的多個按鈕了。如下是在不同的cell上添加一個或兩個左滑按鈕:
//自定義多個左滑菜單選項(xiàng)
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *deleteAction;
deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
[tableView setEditing:NO animated:YES];//退出編輯模式龄广,隱藏左滑菜單
}];
if (indexPath.row == 1) {//在不同的cell上添加不同的按鈕
UITableViewRowAction *shareAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"分享" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
[tableView setEditing:NO animated:YES];//退出編輯模式硫眯,隱藏左滑菜單
}];
shareAction.backgroundColor = [UIColor blueColor];
return @[deleteAction,shareAction];
}
return @[deleteAction];
}
在上述代理方法中我們就可以實(shí)現(xiàn)在cell中添加一個或多個左滑按鈕了,根據(jù)點(diǎn)擊不同的按鈕實(shí)現(xiàn)不同的響應(yīng)方法便可择同。其中[tableView setEditing:NO animated:YES];
方法可以在點(diǎn)擊按鈕之后退出編輯模式并隱藏左滑菜單两入。但如果我們想要修改按鈕的其他屬性如標(biāo)題、背景顏色怎么辦敲才?點(diǎn)擊進(jìn)入UITableViewRowAction
類中裹纳,我們會發(fā)現(xiàn)以下屬性和方法:
@interface UITableViewRowAction : NSObject <NSCopying>
+ (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(nullable NSString *)title handler:(void (^)(UITableViewRowAction *action, NSIndexPath *indexPath))handler;
@property (nonatomic, readonly) UITableViewRowActionStyle style;
@property (nonatomic, copy, nullable) NSString *title;
@property (nonatomic, copy, nullable) UIColor *backgroundColor; // default background color is dependent on style
@property (nonatomic, copy, nullable) UIVisualEffect* backgroundEffect;
@end
其中 @property (nonatomic, readonly) UITableViewRowActionStyle style;
是指設(shè)置所添加按鈕父視圖的背景顏色以及按鈕字體顏色:
typedef NS_ENUM(NSInteger, UITableViewRowActionStyle) {
UITableViewRowActionStyleDefault = 0,//紅底白字
UITableViewRowActionStyleDestructive = UITableViewRowActionStyleDefault,
UITableViewRowActionStyleNormal//灰底白字
} NS_ENUM_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;
而@property (nonatomic, copy, nullable) UIVisualEffect* backgroundEffect;
提供了一個背景模糊效果,有興趣的可以自行研究一下紧武。
上述的方法和屬性只能滿足我們的部分需求剃氧,如果我們想要改變按鈕的大小或者設(shè)置帶圖片的按鈕怎么辦?那就需要我們在視圖中找到我們所要修改的按鈕阻星,并設(shè)置它的各種屬性朋鞍。由于在iOS8-10和iOS11下自定義按鈕處在不同的視圖層次中,所以需要我們先了解UITableView上的視圖層次妥箕。下圖為對比:
從對比圖中可以看出:
(1).iOS10下視圖層次為:UITableView -> UITableViewCell -> UITableViewCellDeleteConfirmationView -> _UITableViewCellActionButton
滥酥,我們所需自定義的按鈕視圖UITableViewCellDeleteConfirmationView(左圖中紅框處)是UITableViewCell的子視圖。
(2).iOS11下視圖層次為:在Xcode 8中編譯為: UITableView -> UITableViewWrapperView -> UISwipeActionPullView -> UISwipeActionStandardButton
畦幢;
在Xcode 9中編譯為: UITableView -> UISwipeActionPullView -> UISwipeActionStandardButton
坎吻。(iOS11中用Xcode 8和Xcode 9中編譯有略微的差別),我們所需自定義的按鈕視圖UISwipeActionPullView(右圖中紅框處)是UITableView的子視圖呛讲。
由于不同系統(tǒng)下的視圖層次不一樣禾怠,因此我們在項(xiàng)目中需要根據(jù)不同的代碼去同時適配iOS8-10和iOS11。
在iOS8-10中( 以下均在Xcode 9中編譯):
在該系統(tǒng)下由于我們所需自定義的按鈕視圖UITableViewCellDeleteConfirmationView是UITableViewCell的子視圖贝搁,所以我們在自定義UITableViewCell子類中遍歷它的subviews即可吗氏。代碼如下:
- (void)layoutSubviews {
/**自定義設(shè)置iOS8-10系統(tǒng)下的左滑刪除按鈕大小*/
for (UIView * subView in self.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
subView.backgroundColor = [UIColor clearColor];//去掉默認(rèn)紅色背景
//設(shè)置按鈕frame
CGRect cRect = subView.frame;
cRect.origin.y = self.contentView.frame.origin.y + 10;
cRect.size.height = self.contentView.frame.size.height - 20;
subView.frame = cRect;
//自定義按鈕的文字大小
if (subView.subviews.count == 1 && self.indexPath.section == 0) {//表示有一個按鈕
UIButton * deleteButton = subView.subviews[0];
deleteButton.titleLabel.font = [UIFont systemFontOfSize:20];
}
//自定義按鈕的圖片
if (subView.subviews.count == 1 && self.indexPath.section == 1) {//表示有一個按鈕
UIButton * deleteButton = subView.subviews[0];
[deleteButton setImage:[UIImage imageNamed:@"login_btn_message"] forState:UIControlStateNormal];
[deleteButton setTitle:@"" forState:UIControlStateNormal];
}
//自定義按鈕的文字圖片
if (subView.subviews.count >= 2 && self.indexPath.section == 0) {//表示有兩個按鈕
UIButton * deleteButton = subView.subviews[1];
UIButton * shareButton = subView.subviews[0];
[deleteButton setTitle:@"" forState:UIControlStateNormal];
[shareButton setTitle:@"" forState:UIControlStateNormal];
[self setUpDeleteButton:deleteButton];
[self setUpShareButton:shareButton];
}
}
}
}
在iOS11中:
在該系統(tǒng)下由于我們所需自定義的按鈕視圖UISwipeActionPullView是UITableView的子視圖,所以我們可以在控制器中自定義UITableView子類中遍歷它的subviews即可(以下方法是寫在UITableView的代理方法- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
中的雷逆,該代理方法每次會在開始左滑按鈕前調(diào)用)弦讽。代碼如下:
/**自定義設(shè)置iOS11系統(tǒng)下的左滑刪除按鈕大小*/
//開始編輯左滑刪除
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger section = indexPath.section;
if (@available(iOS 11.0, *)) {
for (UIView * subView in self.customTableView.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"UISwipeActionPullView")]) {
subView.backgroundColor = [UIColor clearColor];//如果自定義只有一個按鈕就要去掉按鈕默認(rèn)紅色背景
//設(shè)置按鈕frame
for (UIView * sonView in subView.subviews) {
if ([sonView isKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) {
CGRect cRect = sonView.frame;
cRect.origin.y = sonView.frame.origin.y + 10;
cRect.size.height = sonView.frame.size.height - 20;
sonView.frame = cRect;
}
}
//自定義按鈕的文字大小
if (subView.subviews.count == 1 && section == 0) {//表示有一個按鈕
UIButton * deleteButton = subView.subviews[0];
deleteButton.titleLabel.font = [UIFont systemFontOfSize:20];
}
//自定義按鈕的圖片
if (subView.subviews.count == 1 && section == 1) {//表示有一個按鈕
UIButton * deleteButton = subView.subviews[0];
[deleteButton setImage:[UIImage imageNamed:@"login_btn_message"] forState:UIControlStateNormal];;
}
//自定義按鈕的文字圖片
if (subView.subviews.count >= 2 && section == 0) {//表示有兩個按鈕
UIButton * deleteButton = subView.subviews[1];
UIButton * shareButton = subView.subviews[0];
[self setUpDeleteButton:deleteButton];
[self setUpShareButton:shareButton];
}
}
}
}
}
如果我們想在左滑刪除結(jié)束后實(shí)現(xiàn)一些功能,我們可以在UITableView中實(shí)現(xiàn)以下代理方法:
//結(jié)束編輯左滑刪除
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {
}
如果我們想分別設(shè)置UITableViewCell是否需要實(shí)現(xiàn)左滑功能,可以在下面代理方法中實(shí)現(xiàn):
//判斷是否顯示左滑刪除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
在不同系統(tǒng)下分別添加以上代碼即可實(shí)現(xiàn)我們所需要的自定義左滑刪除按鈕往产,效果圖如下:
三被碗、設(shè)置左滑按鈕背景透明色(2024-02-21更新)
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
// 設(shè)置刪除按鈕
if (self.editingIndexPath){
[self configSwipeButtons];
}
}
// 設(shè)置左滑按鈕背景透明色
- (void)configSwipeButtons {
if (@available(iOS 13.0, *)){
// UITableView -> _UITableViewCellSwipeContainerView -> UISwipeActionPullView -> UISwipeActionStandardButton
for (UIView *view1 in self.tableView.subviews){
if ([view1 isKindOfClass:NSClassFromString(@"_UITableViewCellSwipeContainerView")]) {
view1.backgroundColor = UIColor.clearColor;
for (UIView *view2 in view1.subviews) {
if ([view2 isKindOfClass:NSClassFromString(@"UISwipeActionPullView")]) {
view2.backgroundColor = UIColor.clearColor;
for (UIView *view3 in view2.subviews) {
if ([view3 isKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) {
view3.backgroundColor = UIColor.clearColor;
for (UIView *view4 in view3.subviews) {
if ([view4 isKindOfClass:NSClassFromString(@"UIView")]) {
view4.backgroundColor = UIColor.clearColor;
}
}
}
}
}
}
}
}
} else if (@available(iOS 11.0, *)){
// iOS 11層級 (Xcode 9編譯): UITableView -> UISwipeActionPullView
for (UIView *view2 in self.tableView.subviews){
if ([view2 isKindOfClass:NSClassFromString(@"UISwipeActionPullView")]){
view2.backgroundColor = UIColor.clearColor;
for (UIView *view3 in view2.subviews) {
if ([view3 isKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) {
view3.backgroundColor = UIColor.clearColor;
for (UIView *view4 in view3.subviews) {
if ([view4 isKindOfClass:NSClassFromString(@"UIView")]) {
view4.backgroundColor = UIColor.clearColor;
}
}
}
}
}
}
}
}
以上是我總結(jié)整理的在不同系統(tǒng)下的自定義UITableView左滑刪除功能。
如有不足之處仿村,歡迎指正交流锐朴,Demo地址:左滑刪除