Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSFetchedResultsController not updating UITableView's section indexes

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?

like image 631
Luther Baker Avatar asked Jan 22 '23 19:01

Luther Baker


2 Answers

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.

like image 69
Simon Woodside Avatar answered Apr 29 '23 03:04

Simon Woodside


Question already 2 months old, but I ran into the same problem today. It seems like -reloadSectionIndexTitles is not working at all, so I tried a couple of potential hacks which of the following works for me:

@implementation UITableView (JKAdditions)

- (UIView *)indexView {
    Class indexClass = NSClassFromString(@"UITableViewIndex");
    for(UIView *subview in self.subviews){
        if([subview isKindOfClass:indexClass]) return subview;
    }
    return nil;
}

- (void)reloadSectionIndexTitles {
    UIView *indexView = [self indexView];
    [indexView performSelector:@selector(setTitles:) withObject:[self.dataSource sectionIndexTitlesForTableView:self]];
    [indexView setNeedsDisplay];
}

@end

I really have no idea if Apple would reject your App because of this hack, but it seems like the only option for me. Reloading the whole tableView is simply not what I want since I then have to deal with all kinds of animation problems.

I hope this helps anyone having the same problems!

like image 22
Joost Avatar answered Apr 29 '23 03:04

Joost