I'm piggy backing off this previous question that was never answered - Date Section Titles don't work
Can anyone provide code to help me (and the community) properly convert Apple's project DateSectionTitles (http://developer.apple.com/library/ios/#samplecode/DateSectionTitles/Introduction/Intro.html) from Month section titles to Day section titles?
From my numerous attempts, I believe it fundamentally comes down to altering two parts:
(1) RootViewController.m's titleForHeaderInSection
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> theSection = [[fetchedResultsController sections] objectAtIndex:section];
/*
Section information derives from an event's sectionIdentifier, which is a string representing the number (year * 1000) + month.
To display the section title, convert the year and month components to a string representation.
*/
static NSArray *monthSymbols = nil;
if (!monthSymbols) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setCalendar:[NSCalendar currentCalendar]];
monthSymbols = [[formatter monthSymbols] retain];
[formatter release];
}
NSInteger numericSection = [[theSection name] integerValue];
NSInteger year = numericSection / 1000;
NSInteger month = numericSection - (year * 1000);
NSString *titleString = [NSString stringWithFormat:@"%@ %d", [monthSymbols objectAtIndex:month-1], year];
return titleString;
}
(2) and Event.m's sectionIdentifier
- (NSString *)sectionIdentifier {
// Create and cache the section identifier on demand.
[self willAccessValueForKey:@"sectionIdentifier"];
NSString *tmp = [self primitiveSectionIdentifier];
[self didAccessValueForKey:@"sectionIdentifier"];
if (!tmp) {
/*
Sections are organized by month and year. Create the section identifier as a string representing the number (year * 1000) + month; this way they will be correctly ordered chronologically regardless of the actual name of the month.
*/
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit ) fromDate:[self timeStamp]];
tmp = [NSString stringWithFormat:@"%d", ([components year] * 1000) + [components month]];
[self setPrimitiveSectionIdentifier:tmp];
}
return tmp;
}
Thank you in advance!