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

时间:2014-10-14 21:07:32

标签: ios uitableview delete-row

  

我想删除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];
        }
    }];

}

1 个答案:

答案 0 :(得分:0)

由于deleteRowsAtIndexPaths:处理此部分,因此不需要调用tableview来重新加载数据以及开始和更新。因此,您的代码应如下所示:

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

此外,当您试图删除无效的索引路径(pedido)时,您永远不会从数据库中删除对象,因为pedido是一个空的启动的可变数组,不包含任何索引路径。因此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];
  }
}