UITableView 侧滑删除

#pragma mark - 侧滑删除
- (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:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *delete = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"删除动作");
            NSDictionary *item = self.accountList[indexPath.row];
            NSDictionary *params = @{
                                     @"userId":[SRTUser sharedUser].userId,
                                     @"id":item[@"id"],
                                     };
            [[NetWorkManager shareManager] POST:AC_DeleteAccount
                                     parameters:params
                                       progress:nil
                                        success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                                            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                                            NSNumber *status = dict[@"status"];
                                            NSString *msg = dict[@"message"];
                                            if ([status isEqual:@(1)]) {
                                                [SVProgressHUD showSuccessWithStatus:@"删除成功"];
                                                [SVProgressHUD dismissWithDelay:0.8];
                                                [self.tableView.mj_header beginRefreshing];
                                            } else {
                                                [SVProgressHUD showErrorWithStatus:@"删除失败"];
                                                [SVProgressHUD dismissWithDelay:0.8];
                                            }
                                        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                                            [SVProgressHUD showErrorWithStatus:@"删除失败"];
                                            [SVProgressHUD dismissWithDelay:0.8];
                                        }];
        }];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:delete];
        [alert addAction:cancel];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

你可能感兴趣的:(iOS)