I can sort the table view in order of start date using the sort descriptors, but i want to group the sorted tableview into sections with dates for today in one section, dates for next week in another and so forth.
I found this post get NSDate today, yesterday, this Week, last Week, this Month, last Month... variables, that lets me calculate the dates for the events.
Apple has a sample code sorting dates into years, http://developer.apple.com/library/ios/#samplecode/DateSectionTitles/Introduction/Intro.html.
I can't figure out a way to organize the dates into these sections though. If anyone knows a post or sample code that does this, please let me know. Posting a sample code would be appreciated too.
This is what i'm trying to achieve.
Edit: I still haven't figured out exactly how to achieve this, but I'll post the progress so far.
I have can now accurately calculate the dates for this week, next week, etc..
here's the code to do that:
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *today = [NSDate date];
[cal setLocale:[NSLocale currentLocale]];
NSDateComponents *nowComponents = [cal components:NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];
[nowComponents setHour:0]; //8a.m.
[nowComponents setMinute:0];
[nowComponents setSecond:0];
nowComponents = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit fromDate:today];
[nowComponents setMonth: [nowComponents month]]; //This Month begins on
NSDate *thisMonthB = [cal dateFromComponents:nowComponents];
nowComponents = [cal components:NSYearCalendarUnit | NSWeekCalendarUnit fromDate:today];
[nowComponents setWeekday:2]; //Monday
[nowComponents setWeek: [nowComponents week] - 1]; //Last week began on
NSDate *lWeek = [cal dateFromComponents:nowComponents];
nowComponents = [cal components:NSYearCalendarUnit | NSWeekCalendarUnit fromDate:today];
[nowComponents setWeekday:2]; //Monday
[nowComponents setWeek: [nowComponents week]]; //This Week begins on
NSDate *tWeekB = [cal dateFromComponents:nowComponents];
nowComponents = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today];
[nowComponents setDay: 0]; // Yesterday
NSDate *yesterday = [cal dateFromComponents:nowComponents];
nowComponents = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today];
[nowComponents setDay: 1]; // Today
NSDate *tday = [cal dateFromComponents:nowComponents];
nowComponents = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today];
[nowComponents setDay: +2]; // Tomorrow
NSDate *tom = [cal dateFromComponents:nowComponents];
nowComponents = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today];
[nowComponents setDay: +3]; // Tomorrow ends on
NSDate *tomE = [cal dateFromComponents:nowComponents];
nowComponents = [cal components:NSYearCalendarUnit | NSWeekCalendarUnit fromDate:today];
[nowComponents setWeekday:2]; //Monday
[nowComponents setWeek: [nowComponents week] + 1]; //This Week ends on
NSDate *tWeekE = [cal dateFromComponents:nowComponents];
nowComponents = [cal components:NSYearCalendarUnit | NSWeekCalendarUnit fromDate:today];
[nowComponents setWeekday:2]; //Monday
[nowComponents setWeek: [nowComponents week] + 2]; //Next week end on
NSDate *nWeekE = [cal dateFromComponents:nowComponents];
nowComponents = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit fromDate:today];
[nowComponents setMonth:[nowComponents month] + 1];//This Month ends on
NSDate *thisMonthE = [cal dateFromComponents:nowComponents];
NSLog(@"this month began on =%@",[NSDateFormatter localizedStringFromDate:thisMonthB dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle] );
NSLog(@"last week began on =%@",[NSDateFormatter localizedStringFromDate:lWeek dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle] );
NSLog(@"this week begin on =%@",[NSDateFormatter localizedStringFromDate:tWeekB dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle] );
NSLog(@"yesterday=%@",[NSDateFormatter localizedStringFromDate:yesterday dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle] );
NSLog(@"today=%@", [NSDateFormatter localizedStringFromDate:tday dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle]);
NSLog(@"tomorrow=%@",[NSDateFormatter localizedStringFromDate:tom dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle] );
NSLog(@"tomorrow ends on =%@",[NSDateFormatter localizedStringFromDate:tomE dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle] );
NSLog(@"this Week ends on =%@",[NSDateFormatter localizedStringFromDate:tWeekE dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle] );
NSLog(@"next Week ends on =%@",[NSDateFormatter localizedStringFromDate:nWeekE dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle] );
NSLog(@"this Month ends on=%@",[NSDateFormatter localizedStringFromDate:thisMonthE dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle] );
Its a bit different then the code on the link i posted earlier. This code to be much more reliable and accurate.
Now for the section header, If you create a property in your core data object, like sectionTitle with an NSString attribute, and then create an nsmanagedobject subclass, you can create a custom getter for that attribute, like
.m file
(NSString *) sectionTitle {
return @"Today"; }
and in the fetched results controller, when fetching you have an option to placing this attribute for the fetched results controller to place the sections for you, like this:
[[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:[self managedObjectContext]
sectionNameKeyPath:@"sectionIdentifier"
cacheName:@"cache"];
And you will see the section there.
The problem is the table view methods, number of rows in section and title for header in section. I just can't figure out how to place the fetched results into defined sections in the table view.
If anyone can help, please do. I'm lost at how to achieve this, and there seems to be no help or solution out there on how to do this.