在 iOS 8 之前,要給列表加上「右滑出現刪除、置頂、更多」這類按鈕,全靠第三方庫或自己實現。iOS 8 之後系統已經內建了這套能力,一個代理方法加一個類就能搞定。
系統在 UITableViewDelegate 中新增了 tableView:editActionsForRowAtIndexPath:,返回值是一個數組,可以在方法內部創建多個按鈕裝進數組返回。
按鈕的類型是 UITableViewRowAction,通過它設置樣式、標題、背景色以及點擊事件(在 block 中實現)。
數組中先放入的按鈕顯示在最右側,後放入的依次向左排列。
一旦自定義了一個或多個按鈕,系統自帶的刪除按鈕就會消失。
讓 tableView 進入可編輯狀態:
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
UITableViewRowAction 的創建方法:
+ (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style
title:(nullable NSString *)title
handler:(void (^)(UITableViewRowAction *action,
NSIndexPath *indexPath))handler;
然後重寫代理方法:
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView
editActionsForRowAtIndexPath:(NSIndexPath *)indexPath;
下面的例子按行號配置不同的按鈕組合:第 0 行只有刪除,第 1 行是刪除加修改,第 2 行再加發送,其餘行還多一個自定義按鈕。
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
// 添加一個刪除按鈕
deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive
title:@"刪除"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了刪除");
}];
} else if (indexPath.row == 1) {
deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive
title:@"刪除"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了刪除");
}];
// 添加一個修改按鈕
moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal
title:@"修改"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了修改");
}];
moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
} else if (indexPath.row == 2) {
deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive
title:@"刪除"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了刪除");
}];
moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal
title:@"修改"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了修改");
}];
moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
// 添加一個發送按鈕
sanRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal
title:@"發送"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了發送");
}];
sanRowAction.backgroundColor = [UIColor orangeColor];
} else {
deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive
title:@"刪除"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了刪除");
}];
moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal
title:@"修改"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了修改");
}];
moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
sanRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal
title:@"發送"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了發送");
}];
sanRowAction.backgroundColor = [UIColor orangeColor];
// 添加一個自定義按鈕
OK = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal
title:@"OK鍵"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了 OK");
}];
OK.backgroundColor = [UIColor purpleColor];
}
// 將設置好的按鈕放到數組中返回
if (indexPath.row == 0) {
return @[deleteRowAction];
} else if (indexPath.row == 1) {
return @[deleteRowAction, moreRowAction];
} else if (indexPath.row == 2) {
return @[deleteRowAction, moreRowAction, sanRowAction];
} else if (indexPath.row == 3) {
return @[deleteRowAction, moreRowAction, sanRowAction, OK];
}
return nil;
}
樣式只有兩種:Destructive 呈現系統紅色,用於刪除等不可逆操作;Normal 為中性樣式,背景色可自行指定,也可以設 backgroundEffect 做毛玻璃效果,但兩者同時設置時以 backgroundColor 為準,實際開發中建議只選一種。
需要注意的是,UITableViewRowAction 無法自定義內部布局,也無法直接放置圖標。如果設計稿要求圖文混排、自定義字體或特殊圓角,就必須放棄這套 API,轉而使用 iOS 11 引入的 trailingSwipeActionsConfigurationForRowAtIndexPath:(基於 UISwipeActionsConfiguration 與 UIContextualAction,可配 image 與自定義 view)。新項目應優先選擇後者,前者僅為兼容舊系統保留。
另外記得在代理方法中返回 nil 的行,右滑不會有任何響應,這正好可以用來屏蔽不允許操作的行。