一、實現 UITableView 左滑自定義選項
當 UITableView 進入編輯模式,對某個 cell 進行左滑操作時,右側預設會出現 Delete 按鈕。那麼該如何自定義左滑後出現的按鈕呢?其實只要實作 UITableViewDelegate 的以下方法即可。
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction *likeAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"喜歡" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
// 實作相關邏輯
// ...
// 若希望 cell 自動回到預設狀態,需退出編輯模式
tableView.editing = NO;
}];
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"刪除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
// 先改變 model
[self.books removeObjectAtIndex:indexPath.row];
// 接著刷新 view
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
// 更新 view 後會自動退出編輯模式,無須手動處理
}];
return @[deleteAction, likeAction];
}
這樣一來,左滑時就會出現「喜歡」與「刪除」兩個按鈕。它們顯示的先後順序,取決於這個方法所返回陣列中的元素順序。
需要注意的是,一旦實作了上述方法,先前提到的 tableView:commitEditingStyle:forRowAtIndexPath: 與 tableView:titleForDeleteConfirmationButtonForRowAtIndexPath: 就不再被呼叫。若為了相容舊版本,仍需實作 tableView:commitEditingStyle:forRowAtIndexPath:,在裡面什麼都不做即可。
二、UITableview 多行同時刪除
以下程式碼配合 xib 使用,關鍵其實在於後面用到的 delegate。其本質就是对 array 陣列進行刪除操作。
//
// UITableViewDelteMutilRowsViewController.m
// UITableViewDelteMutilRows
//
#import "UITableViewDelteMutilRowsViewController.h"
@implementation UITableViewDelteMutilRowsViewController
@synthesize tableview;
@synthesize dataArray;
@synthesize deleteDic;
@synthesize leftButton;
@synthesize rightButton;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
dataArray = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",nil];
deleteDic = [[NSMutableDictionary alloc] init];
rightButton.title = @"編輯";
}
- (IBAction)choseData{
if (rightButton.title == @"編輯") {
rightButton.title = @"確定";
[self.tableview setEditing:YES animated:YES];
}
else {
rightButton.title = @"編輯";
[deleteDic removeAllObjects];
[self.tableview setEditing:NO animated:YES];
}
}
- (IBAction)deleteFuntion{
[dataArray removeObjectsInArray:[deleteDic allKeys]];
[self.tableview deleteRowsAtIndexPaths:[NSArray arrayWithArray:[deleteDic allValues]] withRowAnimation:UITableViewRowAnimationFade];
[deleteDic removeAllObjects];
}
- (void)dealloc {
[leftButton release];
[rightButton release];
[deleteDic release];
[dataArray release];
[tableview release];
[super dealloc];
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [dataArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (rightButton.title== @"確定") {
[deleteDic setObject:indexPath forKey:[dataArray objectAtIndex:indexPath.row]];
}
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
if (rightButton.title == @"確定") {
[deleteDic removeObjectForKey:[dataArray objectAtIndex:indexPath.row]];
}
}
@end
總結來說,左滑自定義選項靠的是 editActionsForRowAtIndexPath: 回傳一組 UITableViewRowAction;而多選批量刪除則藉由一個字典暫存被選取的行,按下刪除時一口氣從資料陣列中移除並刷新列表,兩者結合就能打造靈活順手的列表操作體驗。