我遇到了一个奇怪的问题。我有一个填充的 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/