0

I have a tableView that is using a NSFetchedResultsController to populate the table cells on viewDidLoad. The user has the option to sort the tableView using a UISegmentControl (index 0 = 'number', index 1 = 'name').

When the user clicks the UISegmentControl it calls a function sortShortListUsingSelectedSortIndex this function will then reorder the tableView using setSortDescriptors and NSFetchedResultsController. When the table has been reordered and a user selects a row the didSelectRowAtIndexPath: is displaying the NSIndexPath that I would expect to see (e.g. IndexPath [0,6] was reordered to Index [0,3]), however when trying to delete that same row the NSIndexPath in didChangeObject:atIndexPath:forChangeType:newIndexPath displays the old IndexPath [0,6] not [0,3] and will cause a crash:

CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (9) must be equal to the number of rows contained in that section before the update (9), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null)

Here is a simplified versions of my methods: Gist

Why am I seeing different indexes for the same row in my tableView?

Joe
  • 2,987
  • 15
  • 24

1 Answers1

0

The fix for this issue was to make sure that my fetch controller was being properly released before trying re alloc. This is the reason the indexPath was using the old index.

[fetchRequestController release];
fetchRequestController = [[NSFetchedResultsController alloc] initWithFetchRequest:shortListRequest
                                                             managedObjectContext:context 
                                                               sectionNameKeyPath:nil 
                                                                        cacheName:nil];
Joe
  • 2,987
  • 15
  • 24