iOS tableview 滑动删除

来源:互联网 发布:数据库程序设计难吗 编辑:程序博客网 时间:2024/05/24 01:49

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

{

    return  UITableViewCellEditingStyleDelete;

}

//先要设Cell可编辑

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

{

    returnYES;

}

//进入编辑模式,按下出现的编辑按钮后

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

{

    [tableView setEditing:NOanimated:YES];

    if (editingStyle ==UITableViewCellEditingStyleDelete) {

        

        UIAlertController * alertController = [UIAlertControlleralertControllerWithTitle:@"提示"message:@"你确定删除该消息?"preferredStyle:UIAlertControllerStyleAlert];

        [alertController addAction:[UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil]];

        [alertController addAction:[UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *_Nonnull action) {

            

            [self.userDataArrremoveObjectAtIndex:indexPath.row];

            [tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];

            

        }]];

        

        [selfpresentViewController:alertControlleranimated:YEScompletion:nil];

    }

}

//修改编辑按钮文字

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

{

    return@"删除";

}

//设置进入编辑状态时,Cell不会缩进

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

{

    returnNO;

}


0 0
原创粉丝点击