commitEditingStyle:删除数据,但继续显示行

我想删除我的tableView的行。 我可以从数据库中删除行,但我的tableview中的行不会消失。 非常感谢!!! Mayte

 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // Delete the row from the data source PFUser *user = [self.allProducts objectAtIndex:indexPath.row]; [_mipedido removeObject:[PFObject objectWithoutDataWithClassName:@"almacen"objectId:user.objectId]]; [[PFUser currentUser] saveInBackground]; NSMutableArray *pedido = [[NSMutableArray alloc] init]; for (int i = 0; i <= 1; i++) { [pedido removeObject:[NSIndexPath indexPathForItem:i inSection:1]]; } [tableView beginUpdates]; [tableView deleteRowsAtIndexPaths:pedido withRowAnimation:NO]; [tableView endUpdates]; } [tableView reloadData]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier =@"editSnacksTableViewCell"; editSnacksTableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; if (cell == nil) { NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"editSnacks" owner:self options:nil]; cell = [nib objectAtIndex:0]; } PFUser *user = [self.allProducts objectAtIndex:indexPath.row]; cell.precio.text = [NSString stringWithFormat:@"%f",[[precioProducto text] doubleValue]]; cell.nombreProducto.text =user[@"nombreProducto"]; // cell.photoSnacks.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]]; // cell.descripcionSnakcs.text =[tableData objectAtIndex:indexPath.row]; return cell; } -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:YES]; self.mipedido = [[PFUser currentUser] objectForKey:@"mipedido"]; PFQuery *query = [self.mipedido query]; // [query whereKey:@"nombreProducto" notEqualTo:self.currentUser.username]; [query orderByAscending:@"nombreProducto"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if (error) { NSLog(@"Error %@ %@", error, [error userInfo]); } else { self.allProducts =objects; [self.tableView reloadData]; } }]; } 

tableview重新加载数据和开始和更新的调用不需要,因为deleteRowsAtIndexPaths:处理这部分。 因此你的代码应该是这样的:

 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // Delete the row from the data source PFUser *user = [self.allProducts objectAtIndex:indexPath.row]; [_mipedido removeObject:[PFObject objectWithoutDataWithClassName:@"almacen"objectId:user.objectId]]; [[PFUser currentUser] saveInBackground]; NSMutableArray *pedido = [[NSMutableArray alloc] init]; //Why use a loop? can't you just put 0 instead of i in the remove object? for (int i = 0; i <= 1; i++) { //pedido is empty as it was just initiated. There is nothing to remove. [pedido removeObject:[NSIndexPath indexPathForItem:i inSection:1]]; } //the NSMutableArray does not contain any index paths so it will not delete anything. [tableView deleteRowsAtIndexPaths:pedido withRowAnimation:NO]; } } 

此外,当您试图删除无效索引path(pedido)时,您将永远不会删除数据库中的对象,因为pedido是空的启动可变数组,不包含任何索引path。 因此tableView不会删除一行。

为了删除用户滑过的indexPath,使用:

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

总的来说,如果您读取的数据库是_mipedido,那么代码应如下所示:

 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // Delete the row from the data source PFUser *user = [self.allProducts objectAtIndex:indexPath.row]; [_mipedido removeObject:[PFObject objectWithoutDataWithClassName:@"almacen"objectId:user.objectId]]; [[PFUser currentUser] saveInBackground]; //delete correct row from tableView [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:NO]; } }