See also
Core Data Sort By Date With FetchedResultsController into sections
Is there a way to group objects from a CoreData entity by date and use them to create multiple sections on a UITableView control?
Essentially, I need something equivalent to the following pseudo-SQL, where my_group_criteria() groups all tests by day:
SELECT * FROM tests GROUP BY my_group_criteria(date)
As an example, the query would group the following rows into 3 separate sections:
[test1 | 2013-03-18 15.30.22]
[test2 | 2013-03-18 14.30.22]
[test3 | 2013-03-18 13.30.22]
[test4 | 2013-03-17 18.30.22]
[test5 | 2013-03-17 19.30.22]
[test6 | 2013-03-15 20.30.22]
As already answered in 2, NSPredicate is not for grouping entities so this may not be the way to go.
Given this, how do I go about creating multiple sections in a UITableView based on some criteria similar to what SQL GROUP BY would do?
I would like to avoid accessing the SQL-lite database directly if at all possible.
Related question 1:
NSPredicate: filtering objects by day of NSDate property
Related question 2:
NSPredicate something equivalent of SQL’s GROUP BY
You can modify the DateSectionTitles sample project from the Apple Developer Library according to you needs.
First, you have to modify the accessor function for the transient
sectionIdentifierproperty to create a section identifier based on year+month+day (instead of year+month only):Second, the
titleForHeaderInSectiondelegate method must be changed, but the idea is the same: extract year, month and day from the section identifer, and create a header title string from that: