0

フェッチされた結果コントローラーにフックされたテーブルビューから行を削除しようとすると、次のエラーが発生します。

* -[UITableView _endCellAnimationsWithContext:]、/SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:1037 2012-07-12 13:11:19.921 Chef[28843:12203] でのアサーションの失敗'、理由: '無効な更新: セクション 0 の行数が無効です。更新後に既存のセクションに含まれる行数 (20) は、更新前にそのセクションに含まれる行数 (20) と等しくなければなりません。そのセクションから挿入または削除された行の数をプラスまたはマイナス (挿入 0、削除 1) し、そのセクションに移動またはセクションから移動された行の数 (0 が移動、0 が移動)。

私のコードは以下です。

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {

    // Delete the record from the DB
    RecipeCategory *objectToBeDeleted = [self.fetchedResultsController objectAtIndexPath:indexPath];
    [self.context deleteObject:objectToBeDeleted];

    // Delete the row from the data source
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];


}   
else if (editingStyle == UITableViewCellEditingStyleInsert) {
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}   
}

フェッチ要求カウントとメソッド (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section を使用して、DB レコードの数と、行の前後のテーブルの行数をログに記録しようとしました[self.context deleteObject:objectToBeDeleted];、両方とも 20 から 19 に減少します。

助けてください!

編集: PS エラーは deleteRowsAtIndexPath メソッドで発生します。

4

1 に答える 1

1

以下のコード行は要件を満たしていないことがわかりました。NSFetchedResultsController もそれを処理すると思います。また、NSFetchedResultsController のデリゲート メソッドも実装する必要がありました。

// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
于 2012-07-12T16:07:17.377 に答える