前言:這篇想聊聊 UITableViewCell 側滑的相關「事」。前幾天逛 GitHub 時看到一個頗有意思的第三方庫,簡單試用後覺得順手,一方面做記錄,一方面希望有類似需求的人能少走冤枉路。
以下分別說明 iOS 5.0 以上(使用第三方庫)與 iOS 8.0 以上(系統原生方法)兩種做法。


使用方式相當簡單,先簽署 MGSwipeTableCellDelegate 協議即可。
這個庫提供了多種 MGSwipeButton 的工廠方法,可依需求挑選:
- (NSArray *)btnLeftCount:(int)count {
NSMutableArray *result = [NSMutableArray array];
UIColor *colors[3] = {[UIColor greenColor],
[UIColor colorWithRed:0 green:0x99/255.0 blue:0xcc/255.0 alpha:1.0],
[UIColor colorWithRed:0.59 green:0.29 blue:0.08 alpha:1.0]};
for (int i = 0; i < count; i++) {
MGSwipeButton *btn = [MGSwipeButton buttonWithTitle:@""
backgroundColor:colors[i]
padding:15
callback:^BOOL(MGSwipeTableCell *sender) {
return YES;
}];
[result addObject:btn];
}
return result;
}
- (NSArray *)btnRightCount:(int)count {
NSMutableArray *result = [NSMutableArray array];
NSArray *titleArray = @[@"刪除", @"標記未讀"];
UIColor *color[2] = {[UIColor redColor], [UIColor lightGrayColor]};
for (int i = 0; i < count; i++) {
MGSwipeButton *btn = [MGSwipeButton buttonWithTitle:titleArray[i]
backgroundColor:color[i]
padding:15
callback:^BOOL(MGSwipeTableCell *sender) {
// 回傳 YES 代表點擊後自動收起,刪除類操作通常希望如此
return i != 0;
}];
[result addObject:btn];
}
return result;
}
可以直接使用 MGSwipeTableCell,也可以自訂一個繼承自它的 cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = @"cellId";
XTCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[XTCustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
}
cell.label.text = [NSString stringWithFormat:@"%ld------%@", (long)indexPath.row, self.arrayTest[indexPath.row]];
cell.label.font = [UIFont systemFontOfSize:20];
cell.delegate = self;
// NO: 同時只能有一個 cell 處於滑開狀態;YES: 多個可同時滑開
cell.allowsMultipleSwipe = NO;
return cell;
}
- (NSArray *)swipeTableCell:(MGSwipeTableCell *)cell
swipeButtonsForDirection:(MGSwipeDirection)direction
swipeSettings:(MGSwipeSettings *)swipeSettings
expansionSettings:(MGSwipeExpansionSettings *)expansionSettings {
if (direction == MGSwipeDirectionRightToLeft) {
return [self btnRightCount:2];
} else {
return [self btnLeftCount:3];
}
}
- (BOOL)swipeTableCell:(MGSwipeTableCell *)cell
tappedButtonAtIndex:(NSInteger)index
direction:(MGSwipeDirection)direction
fromExpansion:(BOOL)fromExpansion {
switch (direction) {
case MGSwipeDirectionLeftToRight: {
NSLog(@"right ------- %ld", (long)index);
break;
}
case MGSwipeDirectionRightToLeft: {
if (index == 0) {
NSIndexPath *path = [_tableView indexPathForCell:cell];
[_arrayTest removeObjectAtIndex:path.row];
[_tableView deleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationLeft];
return NO; // 已自行處理移除,不需再自動收起
}
break;
}
}
return YES;
}
從 iOS 8 開始,系統直接提供了 editActionsForRowAtIndexPath:,不必再引入任何第三方庫:
- (NSArray<UITableViewRowAction> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive
title:@"刪除"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
[self.arrayTest removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}];
UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
title:@"置頂"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
[self.arrayTest exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
[tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
}];
topRowAction.backgroundColor = [UIColor blueColor];
UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal
title:@"更多"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}];
return @[deleteRowAction, topRowAction, moreRowAction];
}
兩種方案的取捨其實很清楚:若專案只需支援 iOS 8 以上,直接用系統 API 最省事,樣式與手勢行為都與系統一致;若需要自訂按鈕外觀、支援展開效果或同時滑開多個 cell,MGSwipeTableCell 這類第三方庫仍舊更靈活。