自定義tableView的左滑編輯,系統(tǒng)默認的為紅色刪除。如果想變成多個按鈕,自定義文字和顏色,在寫完tableView的代理方法之后用下面的方法即可實現(xiàn)。
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"編輯" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
// 實現(xiàn)相關的邏輯代碼
// ...
// 在最后希望cell可以自動回到默認狀態(tài),所以需要退出編輯模式
tableView.editing = NO;
}];
editAction.backgroundColor = [UIColor redcolor];
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"刪除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
// 首先改變model
// [self.model removeObjectAtIndex:indexPath.row];
// 接著刷新view,刪除對應的行
// [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
// 不需要主動退出編輯模式,刷新完成后就會自動退出編輯模式
}];
return @[deleteAction, editAction];
}
“編輯”和“刪除”按鈕的順序跟你最后放到數(shù)組中的順序有關。
**
如果只是實現(xiàn)最簡單的樣式,不妨使用以上的方法,一個方法就可以搞定!
如果想實現(xiàn)相對復雜一些的樣式,可以嘗試下面推薦的三方。
**
https://github.com/CEWendel/SWTableViewCell
它是一個使用起來很簡單的UITableViewCell子類,可以通過左右滑動調(diào)出view,view上有工具按鈕(和iOS 7的系統(tǒng)Mail類似)。

1.png

2.png
實現(xiàn)樣式任您挑選,快去開始你的新工作吧~