1

我的 tableview 数据源来自一个数组,它从托管对象上下文的 executeFetchRequest 方法中获取数据。在 commitEditingStyle 委托中,我收到了这个错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1),

代表:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.managedOjbectContext deleteObject:[self.myEvents objectAtIndex:indexPath.row]];
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [self.tableView reloadData];
    }
}
4

5 回答 5

1

您应该从 NSArray 中删除该条目,然后重新加载 tableView,这样就不会出现不一致...

于 2013-02-21T15:53:35.437 回答
1

错误说明了一切,您删除了一行,但您的代表仍然说它在那里。

我的猜测是你没有从self.myEvents. 从托管上下文中删除对象不会将其从数组中删除。

假设self.myEvents是一个NSMutableArray*

[self.myEvents removeObjectAtIndex:indexPath.row]
于 2013-02-21T15:55:07.240 回答
0

尝试使用 [tableView beginUpdates];[tableView endUpdates];不要reloadData这样使用

[tableView beginUpdates];
if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.managedOjbectContext deleteObject:[self.myEvents objectAtIndex:indexPath.row]];
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }
[tableView endUpdates];
于 2013-02-21T15:57:07.903 回答
0

为什么不封装 and 之间的删除和[self.tableView beginUpdates]所有[self.tableView endUpdates]

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.tableView beginUpdates];
        [self.managedOjbectContext deleteObject:[self.myEvents objectAtIndex:indexPath.row]];
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [self.tableView endUpdates];
    }

}

根据:Apple 的 Table View 编程指南

于 2013-02-21T15:59:02.403 回答
0

如果您从 TableView 中删除一个单元格,则需要更新您的数据源以识别这些更改。

有一个名为 numberOfRowsInSection 的方法:此方法返回表格视图应显示的单元格数。它很可能使用数组或其他类型的数据结构。您需要确保该数组与对表视图本身所做的任何更改保持同步。如果您告诉表视图删除视图,您还需要更新表视图的支持数据以反映更改。

于 2013-02-21T20:25:50.200 回答