It would be rude to do the following:
Add the first sort request to the date attribute to the select request.
Add the "sectionIdentifier" transient property to the Person object, and implement a custom getter - (NSString *)sectionIdentifier to the Person managed object subclass that returns "0", "1", "2", "3" or "4", in depending on the date attribute of the object.
Set sectionNameKeyPath:@"sectionIdentifier" in the creation of the selected result controller.
Add the titleForHeaderInSection method to the table view controller, which returns Today, Tomorrow, ... depending on the section.
DateSectionTitles A sample project from the Apple Developer Library also demonstrates how this works.
Then the sort descriptors will look like this:
// First sort descriptor (required for grouping into sections): NSSortDescriptor *sortByDate = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES]; // Second sort descriptor (for the items within each section): NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:@"firstname" ascending:YES]; [request setSortDescriptors:@[sortByDate, sortByName]];
The getter method for the transition property "sectionIdentifier" will look like this: (adapted from the sample code "DateSectionTitles"):
- (NSString *)sectionIdentifier { [self willAccessValueForKey:@"sectionIdentifier"]; NSString *tmp = [self primitiveValueForKey:@"sectionIdentifier"]; [self didAccessValueForKey:@"sectionIdentifier"]; if (!tmp) { NSDate *date = self.date; // Using pseudo-code here: if ("date is from today") { tmp = @"0"; } else if ("date is from tomorrow") { tmp = @"1"; } else ... // and so on ... [self setPrimitiveValue:tmp forKey:@"sectionIdentifier"]; } return tmp; }
To determine if a date falls today, tomorrow, etc., you should use NSCalendar Methods.
The titleForHeaderInSection method will look like this (unchecked like everything else in this answer):
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { id <NSFetchedResultsSectionInfo> theSection = [[self.fetchedResultsController sections] objectAtIndex:section]; NSString *sectionName = [theSection name]; if ([sectionName isEqualToString:@"0"]) { return @"Today"; } else if ([sectionName isEqualToString:@"1"]) { return @"Tomorrow"; } ...
source share