【问题标题】:commitEditingStyle: delete data but continue to display rowcommitEditingStyle:删除数据但继续显示行
【发布时间】:2014-12-09 19:25:42
【问题描述】:

我想删除我的tableView 的行。我可以从数据库中删除该行,但我的 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];
        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];
        }
    }];

}

【问题讨论】:

  • 当您在后台保存时,可能是在您再次读取数据库之前没有时间保存。尝试同步保存
  • Ignacy,非常感谢您对我的问题感兴趣。实际上,我去更改我的代码,最后我没有任何崩溃,但我继续看到我最近删除的行。我认为这与提神有关。我在 vviewWillAppear 中使用 Parse。我不知道这是否会导致这个问题。提前致谢。
  • pedido中添加的索引路径对象在哪里?
  • 您好 Steven,非常感谢您尝试帮助我。 Pedido 是与“mipedido”相关的 NSMutableArray。我还有另一个属性:allProducts 是数组。我希望能理解你的问题。我在我的问题中添加了更多代码以试图更清楚。真的谢谢。

标签: ios uitableview delete-row


【解决方案1】:

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

【讨论】:

  • 无知。再次非常感谢您尝试帮助我。你说的都是对的。正如你告诉我的那样,我更改了我的代码,但我得到了相同的结果。所以我猜问题出在刷新tableview显示。而且我很害怕,这可能是我写的 viewWillAppear 方法。我附上方法。
  • -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:YES]; self.mipedido = [[PFUser currentUser] objectForKey:@"mipedido"]; PFQuery *query = [self.mipedido query]; [query orderByAscending:@"nombreProducto"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if (error) { NSLog(@"Error %@ %@", error, [error userInfo]); } else { self.allProducts =objects; [self.tableView reloadData]; } }]; }
  • @maytelabarga 查看tableview数据源(cellForRowAtIndexPath)会更有用,您可以将其添加到您的问题中吗?
  • @maytelabarga 您正在从 _mipedido 中删除条目,但使用行 'PFUser *user = [self.allProducts objectAtIndex:indexPath.row];' 从用户读取您需要从数据库中删除用户,因为这是从中读取用户的位置
  • 对不起,Ignacy。我不明白你告诉我的。从 Parse 中删除用户意味着 - 在我的代码中 - PFUser。顺便说一句,我尝试用 mipedido 条目更改这一行,但我无法在 Parse 中删除产品。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-23
  • 1970-01-01
  • 1970-01-01
  • 2010-10-20
  • 2023-03-29
  • 1970-01-01
相关资源
最近更新 更多