Using custom sections with NSFetchedResultsController?
I'm making a sectioned table with fetched results, but am having a hard time getting custom sections worked out.
Normally one would just have an attribute to sort by, and use sectionNameKeyPath:
to generate the sections. But my sorting attribute is calculated on the fly, and I can't seem to get the fetchedResultsController
to use it correctly...
Update: Using jbrennan's advice below, I'm really close to the intended functionality. I've added a Category to NSDate
that returns a "days ago" number; putting that in here gives me sections based on those numbers:
NSFetchedResultsController *aFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:@"myDateAttribute.daysAgo"
cacheName:@"Root"];
Here's where I'm stuck: I don't need them sorted by "days ago," I need them sorted via some calculations based on other attributes in the entity. So I can't just call that custom Category method, I need to call a method with arguments, like so:
[myDateAttribute sortingRoutine:thisObject.value]
Or something like that. I hope that makes some degree of sense. Thanks a ton if you can help :)
You can try the following.
Add a transient attribute to your core data model in the Task entity. Then implement the
- (void)awakeFromFetch
method in your Task NSManagedObject class. See its documentation. Within the method you are allowed to set a value for the transient property using the values of the other properties. Note that there are some restrictions on what you can do, but this is well explained in the documentation (most notably you can not modify relationships or pass arguments; however, if you can compute you transient property using only the values of the other properties/relationships it should be perfectly fine).
Once you have done this, you simply use the transient property as the attribute you pass to get back the sections.
I did something similar to this in a (soon to be) shipping iPhone app. My sections were divided up by dates like this: Yesterday, Today, Tomorrow, In The Future...
Anyway, the trick for me was adding a Category to NSDate to determine in which section my fetched object belonged.
My managed object had a property called dueDate, which was an NSDate. When configuring the fetched results controller, I used @"dueDate.relativeDate"
as the section key path.
In the Category, -relativeDate
was declared as returning an NSString
and also as a readonly
property (either of which might be sufficient, I didn't try without having both, but it doesn't hurt having both a method and property declaration). Then I simply implemented the method at it worked beautifully.
这里有一堆代码来实现这个(谢谢jbrennan):
@implementation NSDate (MyExtensions)
// Return today.
+ (NSDate *)today {
return [NSDate date];
}
// Return yesterday (today minus 24 hours).
+ (NSDate *)yesterday {
return [NSDate dateWithTimeIntervalSinceNow:-60*60*24];
}
// Return tomorrow (today plus 24 hours).
+ (NSDate *)tomorrow {
return [NSDate dateWithTimeIntervalSinceNow:60*60*24];
}
// Convert a date comporting a time into a rounded date (just the day, no time).
- (NSDate *)dayDate {
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *comps = [[NSCalendar currentCalendar] components:unitFlags fromDate:self];
return [[NSCalendar currentCalendar] dateFromComponents:comps];
}
// Return a string representing the date relatively (today, tomorrow, yesterday, etc.)
// If no relative sentence is found, return the NSDateFormatterLongStyle formatted date.
- (NSString *)relativeDate {
if ([self.dayDate isEqualToDate:[[NSDate today] dayDate]]) {
return NSLocalizedString(@"Today", @"NSDate extensions");
}
if ([self.dayDate isEqualToDate:[[NSDate yesterday] dayDate]]) {
return NSLocalizedString(@"Yesterday", @"NSDate extensions");
}
if ([self.dayDate isEqualToDate:[[NSDate tomorrow] dayDate]]) {
return NSLocalizedString(@"Tomorrow", @"NSDate extensions");
}
return [NSDateFormatter localizedStringFromDate:self
dateStyle:NSDateFormatterLongStyle
timeStyle:NSDateFormatterNoStyle];
}
@end
链接地址: http://www.djcxy.com/p/6132.html