平常中所用的tableViewCell,左滑後,編輯狀態只有一個刪除按鈕,現在很多APP上面都自定義實現了cell側滑自定義編輯按鈕,比如qq,微信側滑後有“消息置頂”、“標記爲未讀”等。
話不多說, 代碼如下:
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 添加一個刪除按鈕
UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了刪除");
// 1. 更新數據
NSMutableArray *arrModel = self.dataSource[indexPath.section];
[arrModel removeObjectAtIndex:indexPath.row];
// 2. 更新UI
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}];
// 刪除一個置頂按鈕
UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置頂"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了置頂");
// 1. 更新數據
[self.dataSource exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
// 2. 更新UI
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) {
NSLog(@"點擊了更多");
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}];
moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
// 將設置好的按鈕放到數組中返回
return @[deleteRowAction, topRowAction, moreRowAction];
}