Cell的滑动删除 ios8之后和之前

来源:互联网 发布:通勤车自行车推荐知乎 编辑:程序博客网 时间:2024/05/17 22:20

在Ios8之后这个api实现cell滑动删除方便多了,还可以设置按钮个数以及各种属性:


-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(nonnullNSIndexPath *)indexPath{

    

    UITableViewRowAction *delete = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDefaulttitle:@"删除"handler:^(UITableViewRowAction *_Nonnull action,NSIndexPath *_Nonnull indexPath) {

        

        [_arrayremoveObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:[NSMutableArrayarrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationFade];

    }];   

        UITableViewRowAction *top = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDefaulttitle:@"置顶"handler:^(UITableViewRowAction *_Nonnull action,NSIndexPath *_Nonnull indexPath) {

    

              //...

              

          }];


    top.backgroundColor = [UIColorgrayColor];

    

    return@[delete,top];

    

}





在ios8之前需要调用好些delegate方法:

//系统默认为'delete',修改为'删除'

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

{

    return@"删除";

}

//设置编辑样式为(删除)

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

    returnUITableViewCellEditingStyleDelete;

}

//提交操作

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

    if (editingStyle ==UITableViewCellEditingStyleDelete) {


        [_arrayremoveObjectAtIndex:indexPath.row];

        

        [tableView deleteRowsAtIndexPaths:[NSMutableArrayarrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationFade];

        

    } elseif (editingStyle ==UITableViewCellEditingStyleInsert) {

        //...

    }

}








0 1
原创粉丝点击