iOS 8 之後,列表左滑出按鈕不再需要自己寫手勢,系統提供了 UITableViewRowAction 這套現成 API。本文用一個通訊錄的例子演示如何同時添加「刪除」與「置頂」兩個操作。
#import "AppDelegate.h"
#include "RootTableViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window.rootViewController = [[UINavigationController alloc]
initWithRootViewController:[[RootTableViewController alloc]
initWithStyle:UITableViewStylePlain]];
return YES;
}
#import <UIKit/UIKit.h> @interface RootTableViewController : UITableViewController //存儲數據的可變集合 @property (strong, nonatomic) NSMutableArray *Contacts; // 記錄選中行的索引值 @property (strong, nonatomic) NSIndexPath *CurrentIndexPath; @end
#import "RootTableViewController.h"
@interface RootTableViewController ()
@end
@implementation RootTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"側滑按鈕";
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"106.jpg"]];
//右編輯
self.navigationItem.rightBarButtonItem = self.editButtonItem;
//數據源
self.Contacts = [NSMutableArray array];
[self.Contacts addObject:@"劉木"];
[self.Contacts addObject:@"燕江"];
[self.Contacts addObject:@"唐明芳"];
[self.Contacts addObject:@"王榮政"];
[self.Contacts addObject:@"張雲江"];
[self.Contacts addObject:@"劉茂旭"];
//唯一標識
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"MyCell"];
}
#pragma mark - Table view data source
//多少個分區
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
//每個分區多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.Contacts.count;
}
//單元格重用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"
forIndexPath:indexPath];
//獲取集合內的元素添加到主標題上
cell.textLabel.text = self.Contacts[indexPath.row];
return cell;
}
//實現Cell自定義滑動操作
-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView
editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
//添加一個刪除按鈕
UITableViewRowAction *deleteAction =
[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive
title:@"刪除"
handler:^(UITableViewRowAction * _Nonnull action,
NSIndexPath * _Nonnull indexPath) {
//更新數據
[self.Contacts removeObjectAtIndex:indexPath.row];
//更新 tableview
[tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
NSLog(@"刪除");
[tableView setEditing:NO animated:YES];
}];
//添加按鈕背景色
deleteAction.backgroundColor = [UIColor redColor];
//添加一個置頂按鈕
UITableViewRowAction *topAction =
[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive
title:@"置頂"
handler:^(UITableViewRowAction * _Nonnull action,
NSIndexPath * _Nonnull indexPath) {
//刷新數據:把所選項與第0項交換
[self.Contacts exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
//把所選項置頂
NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
[tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
NSLog(@"置頂");
[tableView setEditing:NO animated:YES];
}];
//設置按鈕的背景色
topAction.backgroundColor = [UIColor grayColor];
//返回我們所設置的按鈕,得以數組的形式返回
return @[deleteAction, topAction];
}
1. 按鈕的顯示順序是數組的逆序
返回的數組是 @[deleteAction, topAction],但屏幕上從左到右呈現的順序與數組相反。這是 iOS 的既定行為,設計稿對不上時先想到這一點。
2. 三種 Style 的含義
UITableViewRowActionStyleDefault:默認樣式;UITableViewRowActionStyleDestructive:破壞性操作, iOS 11 之前左滑完全露出,右滑為淺色;UITableViewRowActionStyleNormal:普通樣式,背景為系統默認灰色。3. 動畫方法的必要性
handler 結尾調用 setEditing:NO animated:YES 是為了讓側滑的按鈕平滑收起。如果不寫,按鈕會生硬地停留在屏幕上。
4. 異步數據更新要小心
如果刪除操作需要等待網絡請求返回,切忌立即調用 deleteRowsAtIndexPaths: 卻還沒從數據源移除數據,兩者不一致會直接崩潰。建議先本地移除並刷新,網絡失敗後再回滾。
5. iOS 11 之後的新寫法
從 iOS 11 開始,蘋果推出了 trailingSwipeActionsConfigurationForRowAtIndexPath: 與 leadingSwipeActionsConfigurationForRowAtIndexPath:,使用 UISwipeActionsConfiguration 和 UIContextualAction。新 API 支持左右兩側同時配置按鈕,並能通過 performsFirstActionWithFullSwipe 控制全滑是否觸發,新項目建議優先採用。
如果需要的不只是幾個按鈕,而是一個複雜的自定義菜單視圖,那 UITableViewRowAction 就不夠用了。常見方案有兩類:一是自己寫 UIPanGestureRecognizer 配合 frame 動畫實現;二是使用成熟的三方庫(如 SWTableViewCell、MGSwipeTableCell)。自研的話,關鍵在於處理好「同一時間只允許一行處於展開狀態」以及與列表滾動的手勢衝突。