1

テーブルビューのデータ ソースは配列から取得され、管理対象オブジェクト コンテキストの 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

エラーはすべてを示しています。行を削除しましたが、デリゲートはまだそこにあると言っています。

私の推測では、オブジェクトをから削除しなかったと思いますself.myEvents。管理対象コンテキストからオブジェクトを削除しても、配列からオブジェクトは削除されません。

と仮定するself.myEventsNSMutableArray*

[self.myEvents removeObjectAtIndex:indexPath.row]
于 2013-02-21T15:55:07.240 に答える
1

NSArray からエントリを削除してから、tableView をリロードする必要があります。そうすれば、矛盾がなくなります...

于 2013-02-21T15:53:35.437 に答える
0

[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のテーブルビュープログラミングガイドライン

于 2013-02-21T15:59:02.403 に答える
0

TableView からセルを削除した場合、これらの変更を認識するためにデータソースを更新する必要があります。

numberOfRowsInSection というメソッドがあります。このメソッドは、テーブル ビューに表示するセルの数を返します。おそらく、配列または他のタイプのデータ構造を使用しています。この配列が、テーブル ビュー自体に加えられた変更と同期していることを確認する必要があります。ビューを削除するようにテーブル ビューに指示する場合は、変更を反映するためにテーブル ビューのバッキング データも更新する必要があります。

于 2013-02-21T20:25:50.200 に答える
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 に答える