當(dāng)UITableView進(jìn)入編輯模式
,在進(jìn)行左滑操作
的cell的右邊
,默認(rèn)會(huì)出現(xiàn)Delete
按鈕,如何自定義左滑出現(xiàn)的按鈕呢?
只需要實(shí)現(xiàn)UITableView下面的這個(gè)代理方法。
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction *likeAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"喜歡" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
// 實(shí)現(xiàn)相關(guān)的邏輯代碼
// ...
// 在最后希望cell可以自動(dòng)回到默認(rèn)狀態(tài),所以需要退出編輯模式
tableView.editing = NO;
}];
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"刪除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
// 首先改變model
[self.books removeObjectAtIndex:indexPath.row];
// 接著刷新view
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
// 不需要主動(dòng)退出編輯模式,上面更新view的操作完成后就會(huì)自動(dòng)退出編輯模式
}];
return @[deleteAction, likeAction];
}
此時(shí)左滑就會(huì)出現(xiàn)兩個(gè)按鈕,一個(gè)是喜歡
,另一個(gè)是刪除
。出現(xiàn)的順序和在這個(gè)方法中返回的數(shù)組中的元素順序相關(guān)。
如果實(shí)現(xiàn)了上述方法,那么之前提到過(guò)的tableView:commitEditingStyle:forRowAtIndexPath:
和tableView: titleForDeleteConfirmationButtonForRowAtIndexPath:
方法就不會(huì)再調(diào)用了。(如果為了兼容以前的版本,那么需要實(shí)現(xiàn)tableView:commitEditingStyle:forRowAtIndexPath:
方法,在這個(gè)方法里什么都不用做即可。)