I have a UITableView that I've set up by following Apple's various Core Data examples. Everything works fine with this view, however, the index on the right hand side only has letters for sections that are actually in the table. I'd like to have all the letters filled in, and if the user taps on a letter that doesn't have a section, take them to the existing section that precedes what they tapped. The Contact app works this way.
Here's what I've written, and it seems to work:
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
NSArray *sections = [fetchedResultsController sections];
for (int i = [sections count] -1; i >=0; i--) {
NSComparisonResult cr =
[title localizedCaseInsensitiveCompare:
[[sections objectAtIndex:i] indexTitle]];
if (cr == NSOrderedSame || cr == NSOrderedDescending) {
NSLog(@"i: %d, title: %@, nearest section: %@, NSComparisonResult: %d",
i, title, [[sections objectAtIndex:i] indexTitle], cr);
return i;
}
}
return 0;
}
Is there an easier/better way to do this?
Will it work properly in languages other than English? I don't know enough about locales/languages to be able to tell.