When sorting a table of objects from Core Data, I’d like to set a custom string for the section heading that includes an attribute. For example, I’d like the section name to display “4 Stars”, instead of just 4. I’ve fiddle with it, but It seems to get grumpy if I try to set the string for the sectionNameKeyPath to anything other than an Entity Attribute and only an entity attribute. Here’s what works for attribute only, and one of a few attempts to customize the string which breaks is commented out.
NSSortDescriptor *ratingDescriptor = [[NSSortDescriptor alloc] initWithKey:@"starRating" ascending:NO];
sortDescriptors = [NSArray arrayWithObjects:ratingDescriptor, nameDescriptor, nil];
[ratingDescriptor release], ratingDescriptor = nil;
// NSString *starSectionHeading = [NSString stringWithFormat:@"%d Stars", @"starRating"];
// sectionKeyPath = starSectionHeading;
sectionKeyPath = @"starRating";
Set your sectionNameKeyPath to the “starRating” but then modify the output in the table view. The FRC will sort things and tidy things up in sections you just have to change what you would normally display as the header string.
I do this in some table views where the output format is handled in a generic fashion (I delegate the header titles to a another controller class given the first sort descriptor path and the value of the title). So you are not limited to hard coding the table view delegate methods like the above code.
You also get a chance to localize the string here as well, I have to deal with 15 localizations in my app and you have to think about things a bit differently when localizing.