Sections on TableView using CoreData
Using Core Data, I have my fetchedResultsController with a with a date field.
I want to display two sections according to the date: if it's a date in the future I want to display it in the section active, otherwise in the non Active section.
Besides I want between the two sections I want to display a "draft section".
It is also important to mention that inside each section, I want them sorted by date.
1) Can I still use the fetchedResultsController? or will I need to create 3 different arrays?
2) Does the field used on sectionN开发者_StackOverflow社区ameKeyPath need to be a String?
Edit:
3) Can I set the order of my sections other than alphabetically?
Edit 2:
for 3) Sections are not ordered alphabetically. How to fix the order of the sections if they're in no way related to the NSSortDescriptor I set to my fetchRequest? Thanks in advance.
Sections need to be defined by some some attribute of the entity whose instances appear in the tableview. Your standard attributes e.g. strings, numbers, dates etc have some built in comparators that make this possible e.g. alphabetical order. The comparators make it possible for the software to determine what object belongs in which section.
However, when you want to add in a highly custom section, such as dividing objects base on whether a data attribute is in the past or future, then you need to provide a comparator of your own.
In this case, you probably want to add a category on NSDate that returns a bool whether a date is in the past or the future. Something like [NSDate isActive]
then you set the sectionNameKeypath of the fetched results controller to theDateAttribute.isActive
. The fetched results controller will then create a section for each value returned by the key, which in this case will be two.
If you provide a date keyed sort descriptor, all the rows will sort by date within their sections.
If you are using custom objects for your NSManagedObjects then all you really need to do is define a method that returns which section it should be in. Not sure what you mean by draft section but as long as the NSManagedObject subclass has a method that generates "Draft" then you should be fine.
To sort by date within a section simply add the sort descriptor to the fetch request. Simple as that.
I've done a similar thing in one of my Apps so if you have any other questions do ask.
You also want to look at subclassing NSFetchedResultsController if you want to customize the creation of sections and index titles. It's all in the docs.
For part 3) there are a couple of ways
- Make your class return names in the display order e.g. @"1)Past" @"2)Present" @"3)Future" and then override the controller:sectionIndexTitleForSectionName: to strip off the 1) 2) etc.
- There is no need to use the default section ordering, you could do a lookup so that when the table asks for section 1 you return the results from section 2 etc.
1 Might seem a bit more kludgy but is probably what I would try first.
精彩评论