您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 8 之後更省事:用 UITableViewRowAction 實作右滑多按鈕選單

編輯:iOS 開發綜合

在 iOS 8 之前,想讓列表項右滑露出「刪除、置頂、更多」這類按鈕,得自己寫手勢、自己算動畫。iOS 8 之後系統把這件事包辦了,一個代理方法加一個類就能搞定。

核心要點

  1. 系統在 UITableViewDelegate 中新增了返回陣列的 tableView:editActionsForRowAtIndexPath:,我們在方法裡建好若干按鈕,裝進陣列返回即可。
  2. 按鈕的型別是 UITableViewRowAction,樣式、標題、背景色與點擊事件(在 block 中實作)都在這裡設定。
  3. 放入陣列的先後順序與顯示位置相反:最先放的在最右邊,最後放的在最左邊。
  4. 一旦自訂了一個或多個按鈕,系統自帶的刪除按鈕就會自動消失,不會與你的按鈕並存。

讓 tableView 進入可編輯狀態

 - (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 上毛玻璃。另外滑出過多按鈕會超出螢幕寬度,超過三個時建議改為「更多」再二次彈出,體驗會好很多。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved