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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to