commitEditingStyle: delete data but continue to di

2019-09-16 18:55发布

问题:

I want to delete the row of my tableView. I can delete the row from database but the row in my tableview does not dissapear. Thanks a lot!!! 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];
        }
    }];

}

回答1:

The calls for the tableview to reload data and begin and updates are not needed because the deleteRowsAtIndexPaths: handles this part. Therefore your code should look like this:

-(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];
  }
}

Also, you will never remove an object from the database as you are trying to remove an invalid index path (pedido) as pedido is an empty initiated mutable array, not containing any index paths. Therefore the tableView will not delete a row.

In order to delete the indexPath the user swiped, use:

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

Overall, the code, if the database that you read from is _mipedido, should look like this:

-(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];
  }
}