「左滑出現刪除按鈕」是 iOS 列表最經典的互動之一。實作起來比想像中簡單,但有一個觀念必須先建立:刪除動作要分成兩半——一半是「從數據源移除」,另一半是「通知表格移除該行」。兩者必須成對出現,否則畫面與數據就會不一致,甚至直接崩潰。本文用一個三筆資料的簡例走完整個流程。
ZYViewController.h:
#import <UIKit/UIKit.h> @interface ZYViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> @property(nonatomic,strong) UITableView *myTableView; @property(nonatomic,strong) NSMutableArray *arryOfRows; @end
ZYViewController.m:
@synthesize myTableView;
@synthesize arryOfRows;
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.myTableView.delegate = self;
myTableView.dataSource = self;
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
arryOfRows = [[NSMutableArray alloc] initWithObjects:@"a", @"b", @"c", nil]; // 表格數據
[self.view addSubview:myTableView];
}
// 每行高度
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGFloat result = 20.0f;
if ([tableView isEqual:self.myTableView]) {
result = 80.0f;
}
return result;
}
// 每個 Section 的行數
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.arryOfRows count];
}
// 每行的數據
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *result = nil;
if ([tableView isEqual:myTableView]) {
static NSString *tableViewCellIdentifier = @"MyCells";
result = [tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];
if (result == nil) {
result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:tableViewCellIdentifier];
}
result.textLabel.text = [arryOfRows objectAtIndex:indexPath.row];
}
return result;
}
// 點擊某一行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([tableView isEqual:myTableView]) {
NSLog(@"%@", [NSString stringWithFormat:@"Cell %ld in Section %ld is selected",
(long)indexPath.row, (long)indexPath.section]);
}
}
// 關鍵一:指定該行的編輯風格為「刪除」
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCellEditingStyle result = UITableViewCellEditingStyleNone; // 預設無編輯風格
if ([tableView isEqual:myTableView]) {
result = UITableViewCellEditingStyleDelete;
}
return result;
}
// 關鍵二:讓表格進入/退出編輯模式
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
[self.myTableView setEditing:editing animated:animated];
}
// 關鍵三:真正執行刪除
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
if (indexPath.row < [self.arryOfRows count]) {
[self.arryOfRows removeObjectAtIndex:indexPath.row]; // 第一步:更新數據源
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationLeft]; // 第二步:更新畫面
}
}
}
那個 indexPath.row < [self.arryOfRows count] 的邊界判斷看似多餘,卻是必要的防護。使用者連續快速滑動刪除時,indexPath 有可能指向已經不存在的索引,少了這層檢查就會直接拋出個數越界異常。
另外,deleteRowsAtIndexPaths:withRowAnimation: 只能呼叫一次對應一次數據變更。若某次操作需要一次刪掉多行,就必須把整批 indexPaths 一起傳入,並在同一次调用中同步移除多筆數據,切勿在迴圈裡逐行呼叫動畫方法。
初始的三筆資料:
在某行上向左滑動,刪除按鈕出現:
點擊刪除後,該行以向左滑出的動畫消失,數據源同步少一筆,畫面與數據維持一致。