在 iOS 8 之前,想讓列表項右滑露出「刪除、置頂、更多」這類按鈕,得自己寫手勢、自己算動畫。iOS 8 之後系統把這件事包辦了,一個代理方法加一個類就能搞定。
- (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{ // 添加一個刪除按鈕 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; }
幾個實務提醒: UITableViewRowActionStyleDestructive 自帶紅色,適合放刪除;Normal 樣式預設是灰色,想自訂就設 backgroundColor,或像範例那樣用 backgroundEffect 上毛玻璃。另外滑出過多按鈕會超出螢幕寬度,超過三個時建議改為「更多」再二次彈出,體驗會好很多。