sam80
sam80

Reputation: 83

Reload table after NSFetchedResultsChangeInsert

This is the next question about my issue with NSFetchedResultsChangeInsert asked before here

The following code is what I use in my NSFetchedResultsController delegate. Update and delete works just fine, excepts when I try to add a new Object to my model.

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;

My question is: How do I update the tableview after insertion? When I hit save, the new object is created. However the table doesn't update accordingly. I've tried everywhere with

[self.tableView reloadData];

I've also tried to call perfomFetch within the ViewWillAppear and ViewDidAppear:

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

But table won't update until I restart the app. I've spent 4 hours looking for an answer but I've got stuck. any help would be much appreciated, thank you

Upvotes: 1

Views: 850

Answers (1)

Martin R
Martin R

Reputation: 539725

There should be no need to call configureCell: for an insert event, [tableView insertRowsAtIndexPaths:...] is sufficient. The table data source method cellForRowAtIndexPath: will be called to get the new cell.

Also, indexPath == nil for insert events, therefore your configureCell: call cannot work.

The important point is to call [tableView beginUpdates] in controllerWillChangeContent: and [tableView endUpdates] in controllerDidChangeContent:.

Upvotes: 2

Related Questions