Iphone – NSFetchedResultsController not updating UITableView’s section indexes

core-dataiphonensfetchedresultscontrolleruitableview

I am populating a UITableViewController with an NSFetchedResultsController with results creating sections that populate section headers and a section index. I am using the following method to populate the section index:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [fetchedResultsController_ sectionIndexTitles];
}

and now I've run into a problem. When I add a new element to the NSManagedObjectContext associated with the NSFetchedResultsController, the new element is saved and appropriately displayed as a cell in the UITableView … except for one thing. If the new element creates a new SECTION, the new section index does not show up in the right hand margin unless I pop the UINavigationController's stack and reload the UITableViewController.

I have conformed to the NSFetchedResultsControllerDelegate's interface and manually invoke

[self.tableView reloadSectionIndexTitles];

at the end of both these delegate methods:

controller:didChangeSection... 
controller:didChangeObject...

and while I can debug and trace the execution into the methods and see the reload call invoked, the UITableView's section index never reflects the section changes.

Again, the data shows up – new sections are physically visible (or removed) in the UITableView but the section indexes are not updated.

Am I missing something?

Best Answer

Looks like this is a bug we're all having. See http://iphonedevelopment.blogspot.com/2009/11/i-know-youre-tired-of-hearing-about.html for what looks to me like a fairly nasty too-many-lines-of-code solution. I went with this:

- (void)viewWillAppear:(BOOL)animated; {
  // This is a dumb hack required by this bug: http://iphonedevelopment.blogspot.com/2009/11/i-know-youre-tired-of-hearing-about.html
  [self.tableView reloadData];
}

It may be inefficient but unless you have reams and reams of data it probably won't do any harm. And it's only 1 line of code. So, when apple fixes their bug, you can easily take it out.