问题
I've got an issue and I'm wondering if it's possible to solve. I have a UITableView that uses Core Data and NSFetchedResultsController. A big part of my app is organizing things based on dates. Ideally, I'd like to divide the TableView into sections based off of 3 date ranges. These ranges would be between now and 2 days, between 2 days and 6 days, and 10 days and beyond. The user has a UIDatePicker, and when they enter a date it would be automatically put into one of these organized sections. Now I know how to easily divide the tableview into sections by each single date, but not how to do it so each section has a time range. Thanks to anyone that might be able to help out.
回答1:
Just did this myself. The end result is that core data objects are sorted into sections, each one being 1 day wide. Multiple objects may be in one section.
//at a point where you define your fetched results controller
//add @"sectionTitle" as sectionNameKeyPath:
//...
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:@"sectionTitle" cacheName:@"CacheName"];
//....
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString* dateString = [[[self.sleepQualityController sections] objectAtIndex:section] name];
return dateString;
}
//add this to your managed object's header file:
@property(nonatomic,assign)NSString* sectionTitle;
//this goes into your managed object's implementation file
-(NSDate *)dateWithOutTime:(NSDate *)datDate
{
if( datDate == nil ) {
datDate = [NSDate date];
}
NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:datDate];
return [[NSCalendar currentCalendar] dateFromComponents:comps];
}
-(NSString*)sectionTitle
{
NSDateFormatter* dateFormaterWeekdayMonthDay = [[NSDateFormatter alloc] init];
dateFormaterWeekdayMonthDay.dateStyle = NSDateFormatterLongStyle;
return [NSString stringWithFormat:@"%@", [dateFormaterWeekdayMonthDay stringFromDate:[self dateWithOutTime:self.date]] ];
}
回答2:
I would explore using NSExpressions in the fetch request. It can be difficult to find good documentation on using SQL-like expressions with fetch requests; but you can also write your own block for the query to use, which is pretty killer.
Basically what you want is an NSExpression which returns a string for the section name, which you can tell the NSFetchedResultsController to use for the section name key path. You will need to build up an NSExpressionDescription around the expression to add it to the fetch request.
I hope this puts you in the right direction!
FWIW, I would normally be inclined to do an SQL-ish solution (essentially selecting a 'case' expression which compares the date field and selects one of three values), but sometimes with Core Data, it's easier to just pull out the sledge hammer (or rather, it feels like that's what they want us to do).
来源:https://stackoverflow.com/questions/11053741/ios-uitableview-sections-based-on-date-ranges