Demo 功能:用 UITableView 實現側滑删除、單擊進入編輯模式、多選行等操作。已在 iPhone、iOS 6.1 環境下測試通過。
Demo 說明:DeleteMeController.m 與 CheckListController.m 都是用 UITableView 實現效果的示例代碼。
Demo 截屏:
#import "DeleteMeController.h"
@implementation DeleteMeController
@synthesize list;
// 切換編輯模式
-(IBAction)toggleEdit:(id)sender
{
[self.tableView setEditing:!self.tableView.editing animated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Initialization code
}
return self;
}
- (void)viewDidLoad
{
// 從 plist 讀取初始數據
NSString *path = [[NSBundle mainBundle] pathForResource:@"computers" ofType:@"plist"];
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];
self.list = array;
UIBarButtonItem *editButton = [[[UIBarButtonItem alloc]
initWithTitle:@"Delete"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(toggleEdit:)] autorelease];
self.navigationItem.rightBarButtonItem = editButton;
[super viewDidLoad];
}
#pragma mark - Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [list count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *DeleteMeCellIdentifier = @"DeleteMeCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DeleteMeCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:DeleteMeCellIdentifier] autorelease];
}
NSInteger row = [indexPath row];
cell.textLabel.text = [self.list objectAtIndex:row];
return cell;
}
#pragma mark - Table Delegate Methods
// 提交删除:同時更新數據源與界面
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
[self.list removeObjectAtIndex:row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
@end
1. 進入編輯模式的兩種方式。 代碼裡通過導航欄按鈕調用 setEditing:animated: 主動切換;而側滑删除默認就是開放的——只要實現了 commitEditingStyle:forRowAtIndexPath:,系統會自動為每一行加上側滑手勢,無需額外代碼。這也是很多人困惑「為什麼我的表格不能側滑」的原因:不是缺手勢,而是缺這個方法。
2. 數據源與界面必須同步更新。 這是最容易出錯的地方。在 commitEditingStyle: 中,先從數組移除對象,再調用 deleteRowsAtIndexPaths:withRowAnimation:。順序反了會導致行數與數據個數不一致,直接拋出 NSInternalInconsistencyException。如果删除後數據源沒變,界面動畫雖然播放了,但下一次 reloadData 時被刪的行又會回來。
3. 判斷 editingStyle。 嚴謹的寫法應該先判斷樣式,只處理删除:
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.list removeObjectAtIndex:[indexPath row]];
[tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
}
4. 批量删除更省心。 從 iOS 8 起,可以直接返回一個方法作為删除 handler:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.items removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
5. 多選模式。 若要實現清單式的多選(左側出現選中圈),需要打開表格的 allowsMultipleSelectionDuringEditing,並在 didSelectRowAtIndexPath: 中收集選中的 indexPath;取消時在 willDeselectRowAtIndexPath: 中移除。iOS 13 之後蘋果提供了 UITableViewDiffableDataSource 與 UICollectionViewCompositionalLayout 等更現代的 API,能大幅簡化增刪改的動畫與狀態管理,新項目建議優先採用。
把「實現 commitEditingStyle → 同步更新數據源 → 調用 deleteRows」這條主線記牢,UITableView 的删除交互就完全掌握在手了。