How can I change section ordering with NSFetchedResultsController?

I'm setting up an NSFetchRequest with the following sort descriptor:

[sortDescriptors addObject:[NSSortDescriptor sortDescriptorWithKey:@"color" ascending:YES]];

I'm setting the sectionNameKeyPath for the NSFetchedResultsController to be the "color" attribute.

The results are sorted into sections based on color, as I would expect.

It's valid for the "color" attribute to be an empty string "" for some objects. Those are all grouped together in the first section, since they sort before all other names. However, I'd like them to appear as the final section, not the first section, without affecting the search order of the remaining objects.

For example, currently the sections might be:

 ""
 "green"
 "red"
 "yellow"

But I'd like the sections to be in the order:

 "green"
 "red"
 "yellow"
 ""

Is there some way to write the sort descriptor to get this behavior? I've been unable to come up with anything myself.


A sort descriptor for a (SQLite based) Core Data fetch request can only use some standard comparator methods and only use (persistent) attributes stored in the database. To get a non-standard order you will have to add an additional attribute to the entity and use that for sorting.

链接地址: http://www.djcxy.com/p/36102.html

上一篇: self.tableView.delegate = self和self.table.dataSource =自我内存泄漏

下一篇: 我如何使用NSFetchedResultsController更改节的顺序?