NSFetchedResultsController: different sort descriptors for different sections
NSFetchedResultsController: different sort descriptors for different sections
- Subject: NSFetchedResultsController: different sort descriptors for different sections
- From: WT <email@hidden>
- Date: Sun, 5 Dec 2010 18:02:27 -0200
I have a situation where there are two sections in a table view. The first section should have its rows sorted according to a certain index stored in the managed object, while the second section should have its rows sorted alphabetically by the name of its managed objects.
How can I create a *single* fetched results controller that sorts different sections differently? From reading the docs for NSFetchedResultsController, in a situation with several sections, the first sort descriptor is responsible for splitting the data into sections. Fine, by I can't seem to think of a way to conditionally set the remaining sort descriptors according to which section the rows fall into.
Here's part of the code I have now:
NSEntityDescription* entity = [NSEntityDescription
entityForName: @"<entity name>"
inManagedObjectContext: self.managedObjectContext];
NSSortDescriptor* sortBySectionName = [[NSSortDescriptor alloc]
initWithKey: @"sectionName"
ascending: YES];
NSSortDescriptor* sortByIndex = [[NSSortDescriptor alloc]
initWithKey: @"index"
ascending: YES];
NSArray* sortDescriptors = [[NSArray alloc]
initWithObjects: sortBySectionName, sortByIndex, nil];
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity: entity];
[fetchRequest setSortDescriptors: sortDescriptors];
It works, but it sorts both sections by the managed object's index. The only alternative I can think of is to create *two* fetched results controllers, one as above and the other sorting by section name and then object name (rather than index). Then, when it comes time to feed the table view, have some logic that selects data from the appropriate controller. That sounds too cumbersome. What if I had several more sections, each needing different sorting criteria? There's got to be an easier way.
Thanks in advance.
WT_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden