Hi There,
I was wondering if anyone knew the best solution to defining a custom display order for the sections in a UITableView - I am loading the data using Core Data and an NSFetchedResultsControler, and I am successfully using NSSortDescriptors and also successfully grouping the items returned into sections using sectionNameKeyPath:
So far so good BUT - the sections are listed in alphabetical order and I would like to override this and order the sections in a specific custom sort order.
For example if I had a Entity called Item with an attribute called colour and I used this colour attribute as the sectionNameKeyPath: how would I then order the sections in a specific way - currently the sections display as
Blue, Green, Indigo, Orange, Red, Violet, Yellow.
- because they have an NSortDescriptor applied to the NSFetchedResultsController (which I have defined). BUT how could I reorder these sections in a specific way to display the sections like so -
Red, Orange, Yellow, Green, Blue, Indigo, Violet.
My NSFetchedResultsController code follows -
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController_ != nil) {
return fetchedResultsController_;
}
/*
Set up the fetched results controller.
*/
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *colourDescriptor = [[NSSortDescriptor alloc] initWithKey:@"colour" ascending:YES];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:colourDescriptor, sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"colour" cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[colourDescriptor release];
[sortDescriptors release];
NSError *error = nil;
if (![fetchedResultsController_ performFetch:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return fetchedResultsController_;
}
I understand the examples posted regarding using a custom subclass of NSManagedObject and a getter method to dynamically assign a property to be used as the sectionNameKeyPath: but the examples for these still tend to provide section names which are then listed alphabetically.
It's clearly me being new to this but any suggestions/direction would be greatly appreciated.
Thanks.