在 iOS 8 之前,想要在列表上做出「右滑出現刪除、置頂、更多」這類按鈕,每一項都得自己寫手勢與動畫。從 iOS 8 開始,系統已經把這件事包好了,只要實作一個代理方法,再搭配 UITableViewRowAction 這個類別即可。
1、iOS 8 新增的代理方法是 tableView:editActionsForRowAtIndexPath:,回傳值是一個陣列。你可以在方法裡建立好幾個按鈕,裝進陣列再回傳。
2、每個按鈕的型別是 UITableViewRowAction,可以設定樣式、顯示文字、背景色,以及點擊後要做什麼(寫在 handler 這個 block 裡)。
3、回傳陣列時有個順序規則:最先放進陣列的按鈕顯示在最右側,最後放的最靠左。
4、只要你自訂了一個以上的按鈕,系統原生的「刪除」按鈕就會自動消失。
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
+ (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(nullable NSString *)title handler:(void (^)(UITableViewRowAction *action, NSIndexPath *indexPath))handler;
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
- (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 {
// 刪除 + 修改 + 發送 + OK
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;
}
回傳 nil 或空陣列,該列就不會有滑動操作,這在「某些列不允許刪除」的場景很好用。
另外,UITableViewRowActionStyleDestructive 會自動套用系統的紅色,而 Normal 樣式預設是灰色;若像範例那樣自訂 backgroundColor,backgroundEffect 就會被忽略,兩者只能擇一。
最後記得,點擊 handler 只是回報「使用者按了哪一顆按鈕」,真正的資料刪除與列表 reload 仍要你自己在 block 裡處理,並記得對 index 做有效性檢查,避免資料已經變動導致陣列越界。
以上就是 iOS 中使用 tableView 實作右滑操作按鈕的整理,希望對大家有所幫助。