本文授權轉載,作者:@夏天是個大人了
前言:這篇其實就是想聊聊 tableview cell 滑動的那些「事」。昨天逛 GitHub 時翻到一個挺有意思的三方庫,簡單上手試了試,感覺不錯,一是做個記錄,二是希望有類似需求的人能少走點彎路。
下面分別介紹 iOS 5 之後(三方庫)和 iOS 8 之後(系統 API)兩種實現方式。

效果圖 - iOS >= 5.0

效果圖 - iOS >= 8.0
接入很簡單,直接看代碼。首先簽一個 MGSwipeTableCellDelegate 協議。
添加左側按鈕
- (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) {
BOOL autoHide = i != 0;
return autoHide;
}];
// 把按鈕加到數組中
[result addObject:btn];
}
return result;
}重用池的寫法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = @"cellId";
// 這裡如果 MGSwipeTableCell 就夠用,可以直接使用
// 或者自定義創建 cell 並繼承自 MGSwipeTableCell,像下面這樣
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: 只有單個可以滑動,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: {
if (index == 0) {
NSLog(@"right ------- 0");
}else{
NSLog(@"right ------- 1");
}
break;
}
case MGSwipeDirectionRightToLeft: {
if (index == 0) {
NSLog(@"left ------- 0");
// 這裡簡單做了個刪除操作
NSIndexPath * path = [_tableView indexPathForCell:cell];
[_arrayTest removeObjectAtIndex:path.row];
[_tableView deleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationLeft];
return NO;
}else{
NSLog(@"left ------- 1");
}
break;
}
}
return YES;
}系統在 iOS 8 提供了類似能力,實現起來更省事:
- (NSArray*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { [self.arrayTest removeObjectAtIndex:indexPath.row]; [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; }]; UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置頂" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull 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 * _Nonnull action, NSIndexPath * _Nonnull indexPath) { [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle]; }]; return @[deleteRowAction, topRowAction, moreRowAction]; }