私のアプリケーションには、ルートコントローラとしてUITableViewControllerがあり、このテーブルに行を追加するためのモーダルビューがあります。 CoreDataを使用しているので、NSFetchedResultsControllerからデータを取得します。とにかく、問題は、UITableViewの管理にあります。アニメーションを含む最初の行に挿入すると、UITableViewがセルを正しく表示しない
各挿入により、TableViewは新しいセルを追加します。セルが最初の行に追加されている場合を除き、すべて正常に動作します。この場合、そのコンテンツは表示されません。セルに空白が表示されます。セルをタップするか、セルを再読み込みする必要があるようにテーブルをスクロールすると、コンテンツが表示されます。
私は[tableView beginUpdates]と[tableView endUpdates]を[docView reloadData]の代わりにApple docsが言ったようにアニメーションを使用しています。 [tableView reloadData]を実行するとすべて正常に動作します。
私はそれをチェックし、すべての行に対して同じコードが実行されています。細胞の表示方法は問題です。
私はこの問題はTableViewsでアニメーションの「理論」についてだと思う、あなたはおそらくそれを必要としませんが、私のUITableViewControllerの関連するコードがある:
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
Customer *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = managedObject.name;
}
#pragma mark - Table view data source
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
#pragma mark - FetchedResultsController delegate
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
// [self.tableView reloadData]; this make all works OK but without animations
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
ケースNSFetchedResultsChangeInsertにブレークポイントを配置してコードをデバッグしましたが、newIndexPathを記録しようとしました。 –