commitEditingStyle:删除数据但继续显示行 [英] commitEditingStyle: delete data but continue to display row

查看:79
本文介绍了commitEditingStyle:删除数据但继续显示行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


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

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

}


推荐答案

不需要重新加载表视图以重新加载数据,开始和更新,因为deleteRowsAtIndexPaths:处理了这一部分。因此,您的代码应如下所示:

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

此外,您绝不会从数据库中删除对象由于您要删除无效的索引路径(pedido),因为pedido是一个空的初始化可变数组,不包含任何索引路径。因此,tableView将不会删除行。

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.

要删除用户滑动的indexPath,请使用:

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

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

总体而言,如果您从中读取的数据库是_mipedido,则代码应如下所示:

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

这篇关于commitEditingStyle:删除数据但继续显示行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆