1

これは、ここで前に尋ねられた NSFetchedResultsChangeInsert に関する私の問題に関する次の質問です

次のコードは、NSFetchedResultsController デリゲートで使用するものです。モデルに新しいオブジェクトを追加しようとする場合を除いて、更新と削除は問題なく機能します。

case NSFetchedResultsChangeInsert:
           [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            [self configureCell:[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:1]] atIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:1]];            
            break;

私の質問は: 挿入後にテーブルビューを更新するにはどうすればよいですか? 保存を押すと、新しいオブジェクトが作成されます。ただし、テーブルはそれに応じて更新されません。私はどこでも試しました

[self.tableView reloadData];

また、ViewWillAppear と ViewDidAppear 内で perfomFetch を呼び出そうとしました。

NSError *error = nil;
if (![_fetchedResultsController performFetch:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

ただし、アプリを再起動するまでテーブルは更新されません。答えを探すのに4時間費やしましたが、行き詰まりました。どんな助けでも大歓迎です、ありがとう

4

1 に答える 1

2

configureCell:挿入イベントを呼び出す必要はなく、[tableView insertRowsAtIndexPaths:...]十分です。新しいセルを取得するために、テーブルデータソースメソッドcellForRowAtIndexPath:が呼び出されます。

また、indexPath == nil挿入イベントの場合、configureCell:呼び出しは機能しません。

重要なポイントは、を呼び出すこと[tableView beginUpdates]です。controllerWillChangeContent:[tableView endUpdates]controllerDidChangeContent:

于 2012-07-26T20:18:57.017 に答える