Reputation: 2072
When you want to do a sectioned tableview (using core data) you have to somehow provide the corresponding text for the section(s) header; one way of doing that is through having an array with strings which can be indexed by the section index.
I've seen for many scenarios building such array is fairly simple or sometimes straight forward.
However, I have not found an [effective] way of bulding that "sections" array of strings if what you want/need to separate into sections is your set of entity instance objects and the header consists of concatenating the values of more than one attribute of your object.
Usually you will have your entity objects in another array which is the data source for the same tableview. And I have that array already sorted/grouped by the specific criteria I want sections for.
When your sections are as simple as the value of ONLY ONE of the attributes in the instance, you can simply filter your array of objects and get all the distinct values of one attribute and use those in the sections array and you are done.
How do you usually create the sections array when MORE THAN ONE ATTRIBUTE in each instance object needs to be used as the text for the section. Do you concatenate those values you are interested in being part of the section header?? That's what I'm doing and then filtering the array that holds that concatenated string but, would like to know whether that's right or if there's another "better" way to create the sections.
For instance, right now I'm concatenating 3 attributes in the instance: a date, a title, and a name (11/02/2012 GROCERIES Frute). Since my instance objects are sorted by date, title, name, some objects belong to the same section while other objects belong to another section and so on. The sections array is supossed to only have the concatenated strings that are distinct, so I filter it to eliminate duplicates and I end up with the sections array.
Still, I feel like there should be a better way.
Upvotes: 0
Views: 215
Reputation: 1430
When I'm building a sections-array on a value or result that is not sortable, or concatenated of mulitiple values, I always create a custom sort-value to pass as the first sortdescriptor to the NSFetchedResultsController
.
In order to get the correct "GROUP BY"-SQL syntax generated by Core Data, you need to have the same attribute in both the SectionNameKeyPath and the first sortdescriptor.
From there, I override the – controller:sectionIndexTitleForSectionName:
method in the NSFetchedResultsControllerDelegate
, and concatenate or return the section title for the given section:
- (NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
NSArray *articles = [sectionInfo objects];
if ([articles count] > 0)
{
Article *article = [articles objectAtIndex:0];
return article.section;
}
return [sectionInfo name];
}
Upvotes: 1