I currently have a class which has a date object in it. This date object has both time and day in it. All this information gets loaded into a UITableViewCell
via a
You may want to check out the Core Data Sample called "DateSectionTitles" for more insight, I'm highlighting the basics from it.
To have sections according to date, you need to first sort by a normalized (or simplified) date. Try the following:
Add a transient attribute to your entity in the core data model editor. Ensure that it's type is NSDate and it's transient property is checked. Update your NSManagedObject class files by adding the property as a strong NSDate. Call it, "simplifiedDate". Also add another property to your .h file, call it "primitiveSimplifiedDate". Make it a strong.
In the .m use @dynamic for both the simplifiedDate and primitiveSimplifiedDate. This transient attribute should use return an NSDate that has been normalized to midnight. This level sets everything and allows you to establish the sections. I've used code along the lines of this:
-(NSDate *)simplifiedDate{
// Create and cache the section identifier on demand.
[self willAccessValueForKey:@"simplifiedDate"];
NSDate *tmp = [self primitiveSimplifiedDate];
[self didAccessValueForKey:@"simplifiedDate"];
if (!tmp) {
tmp=[self simplifiedDateForDate: [self due]];
[self setPrimitiveSimplifiedDate: tmp];
}
return tmp;
}
-(NSDate *)simplifiedDateForDate:(NSDate *)date {
if (!date) {
return nil;
}
static NSCalendar *gregorian = nil;
gregorian=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *newDateComponents=[gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit| NSDayCalendarUnit) fromDate:date];
return [gregorian dateFromComponents:newDateComponents];
}
If you so desired, this could also be an actual attribute that is calculated and stored when the actual date is set.
Next, you will have two sort descriptors. The first sort descriptor will be the above transient attribute, "simplifiedDate". The second attribute will be the "due" attribute. The "simplifiedDate", so long as the NSFetchedResultsController is setup correctly in the next step, will sort everything according to the section.
Finally you need to provide the section key name when you alloc/init the NSFetchedResultsController. In this case it will be "simplifiedDate". Like this:
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"simplifiedDate"cacheName:@"Root"];
Hope this helps.
t