ios tableView滑动删除(单个提示和多个提示)

ios 在tableView的滑动删除方面封装的相当不错,几段代码就能实现效果,下面先说下单组删除


1. 单个提示删除

首先要设置cell可编辑,这是tableView的代理方法

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

return YES;

}

然后在这个方法里写上你要做的事(我这里边写的是提醒提示框是否删除)

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

if (editingStyle == UITableViewCellEditingStyleDelete) {

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要删除?删除后无法恢复!" preferredStyle:1];

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}];

UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

[alert addAction:okAction];

[alert addAction:cancel];

[self presentViewController:alert animated:YES completion:nil];

}

}

最后你可以在这个代理方法里边修改文字


- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

{

return @"删除";

}


2 .多个提示删除(我这里提示的是取消关注和设为默认)

- (NSArray*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath

{

UITableViewRowAction *deleteRoWAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"取消关注" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

}];//此处是iOS8.0以后苹果最新推出的api,UITableViewRowAction,Style是划出的标签颜色等状态的定义,这里也可自行定义

UITableViewRowAction *editRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"设为默认" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

}];

deleteRoWAction.backgroundColor = [UIColor redColor];//可以定义RowAction的颜色

editRowAction.backgroundColor = XABBackBroudBlue;

return @[editRowAction, deleteRoWAction];//最后返回这俩个RowAction 的数组

}

你可能感兴趣的:(ios tableView滑动删除(单个提示和多个提示))