ios 8 以前使用的方法:
//设Cell可编辑
- (BOOL)tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath {
returnYES;
}
//定义样式
- (UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath {
returnUITableViewCellEditingStyleDelete;
}
//修改按钮文字
- (NSString*)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexPath {
return@"删除";
}
//删除相应方法
- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath {
//在这里实现删除操作
}
ios 8 到 ios11使用如下方法:
//ios 11之前的方法- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewRowAction *a1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
// 按钮被按了,会调用block里面的代码:
NSLog(@"置顶");
}];
UITableViewRowAction *a2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
// 按钮被按了,会调用block里面的代码:
NSLog(@"删除");
}];
UITableViewRowAction *a3 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"收藏" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
// 按钮被按了,会调用block里面的代码:
NSLog(@"收藏");
}];
a3.backgroundColor = [UIColor greenColor];
return @[a1, a2, a3];
}
ios11之后使用的方法
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath{
UIContextualAction *deleteAction = [UIContextualAction
contextualActionWithStyle:UIContextualActionStyleDestructive
title:@"删除"
handler:^(UIContextualAction * _Nonnull action,
__kindof UIView * _Nonnull sourceView,
void (^ _Nonnull completionHandler)(BOOL))
{
// [self.tableView setEditing:NO animated:YES]; // 这句很重要,退出编辑模式,隐藏左滑菜单
// [self.arr removeObjectAtIndex:indexPath.row];
// [_table deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
// [_table reloadData];
completionHandler(true);
}];
deleteAction.backgroundColor = [UIColor grayColor];
UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction]];
actions.performsFirstActionWithFullSwipe = NO;
return actions;
}
网友评论