Amusing name for an awesome iOS blog I just came across recently: CIMFG.com
Their list of code snippets seem handy: http://cimgf.com/cocoa-code-snippets
So, I’ve added NSFetchResultsController into my table view and my rows are all jiving nicely with the model. Sweet.
But now some of my rows are screwed up between the two sections at times. When I try to add a new goal and then I complete it, it crosses out and moved to the bottom section as expected. However, when I do that with an existing goal for some reason it doesn’t render and move rows, even though the completed attribute’s getting logged in console.
- (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:(RegimenCell *)[_tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[_tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
break;
}
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray
arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
This is one of the methods that get picked up on automatically in NSFetchResultsController whenever something changes in the model. What’s tripping me is that sometimes moved objects are being reported as updated instead. I tried the work-around there too, simply accounting for it by picking up on whether the goal is completed or not and it goes to the NSFetchedResultsChangeMove handler alright, but it’s not putting it into a new row – the newIndexPath is still the same as the old index path for some reason. Hmmm…