ios - 在 rowindexpath 的 tableView 中删除项目会删除它下面的项目吗?

标签 ios objective-c uitableview

我遇到了一个奇怪的问题。我有一个填充的 TableView ,允许用户通过向右滑动并点击“删除”来删除项目。该功能运行良好,成功地从表和数据源(Drupal 节点)中删除了项目。但是,出于某种原因,当我滑动某个项目以将其删除时,它实际上删除了我在数据源中选择的项目的 UNDERNEATH。 例如 我选择删除我的 TableView 中的节点 133,而节点 132 是被删除的。

我创建了一个字符串,用于从 Drupal 中获取选定行的节点 ID,但它几乎就像是在我选定的行中获取节点 ID 之前的节点 ID...

见代码:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [self.descripData removeObjectAtIndex:indexPath.row];

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

        NSMutableDictionary *nodeData = [[self.descripData objectAtIndex:indexPath.row] mutableCopy];
        NSString *nid = [nodeData objectForKey:@"nid"];
        [nodeData setObject:nid forKey:@"nid"];
        [DIOSNode nodeDelete:nodeData success:^(AFHTTPRequestOperation *operation, id responseObject) {
         NSLog(@"node deleted!");
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"could not delete node!");
       }];

        [tableView reloadData];
    }

}

最佳答案

问题是您从 self.descripData 中删除数据后 获取 nodeData。所以实际上你确实获取了下一行的数据。颠倒顺序。

此外,不要调用 reloadData。您已经从表中删除了已删除的行。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSMutableDictionary *nodeData = [[self.descripData objectAtIndex:indexPath.row] mutableCopy];

        // Delete the row from the data source
        [self.descripData removeObjectAtIndex:indexPath.row];

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

        NSString *nid = [nodeData objectForKey:@"nid"];
        [nodeData setObject:nid forKey:@"nid"];
        [DIOSNode nodeDelete:nodeData success:^(AFHTTPRequestOperation *operation, id responseObject) {
         NSLog(@"node deleted!");
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"could not delete node!");
       }];
    }
}

关于ios - 在 rowindexpath 的 tableView 中删除项目会删除它下面的项目吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30312013/

相关文章:

objective-c - 在 Cocoa 应用程序中嵌入 Web 服务器

objective-c - 如何在没有实例的情况下获取类对象?

objective-c - atebits Twitter 快速滚动

ios - 圆形 UIImageView 在某些设备上是方形的

ios - 将数据从 UItableviewController 传递到 UItableViewcell

ios - 从另一个 VC 打开一个 VC,而不将它们推送到导航堆栈 (Swift)

ios - iOS CoreData fetchRequest具有顺序列变音符号敏感

ios - UITableView - 调整框架大小并滚动到行

ios - 无法在 Interface Builder 的单元格中移动内容 View

ios - Web View 中的编程后退按钮 (WebKit)