所以我有这个自定义生成的数组来定义我的部分UITableView
,但是你可以看到所有的项目都是通过核心数据获取并单独排序的。
这是我的自定义代码:
-(NSArray *)tasks {
NSManagedObjectContext *managedObjectContext = [self.fetchedResultsController managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Task" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
// retrive the objects with a given value for a certain property
[request setPredicate:nil];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"scheduledDate" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
NSError *error = nil;
NSArray *result = [managedObjectContext executeFetchRequest:request error:&error];
if ((result != nil) && ([result count]) && (error == nil)){
return [NSMutableArray arrayWithArray:result];
}
return [NSMutableArray array];
}
-(NSMutableArray *)sectionsArray {
// Fetch the scheduledDate check if it is in any of the term arrays and dump it in that section.
NSArray *tasks = [self tasks];
if (tasks) {
NSArray *scheduledTasks = [tasks valueForKey:@"scheduledDate"];
scheduledTasks = [[[NSSet setWithArray:scheduledTasks] allObjects] sortedArrayUsingSelector:@selector(compare:)];
NSMutableArray *shortTermTasks = [NSMutableArray array];
NSMutableArray *midTermTasks = [NSMutableArray array];
NSMutableArray *longTermTasks = [NSMutableArray array];
for (NSDate *scheduledDate in scheduledTasks) {
NSString *dateString = [NSString dateStringFromFullDate:scheduledDate];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"scheduledDate == %@", scheduledDate];
NSManagedObject *task = [[tasks filteredArrayUsingPredicate:pred] lastObject];
if ([[self shortTermDateStrings] containsObject:dateString]) {
[shortTermTasks addObject:task];
}
if ([[self midTermDateStrings] containsObject:dateString]) {
[midTermTasks addObject:task];
}
if ([[self longTermDateStrings] containsObject:dateString]) {
[longTermTasks addObject:task];
}
}
sectionsArray = [NSMutableArray arrayWithObjects:shortTermTasks,midTermTasks,longTermTasks, nil];
return sectionsArray;
}
return [NSMutableArray array];
}
我研究并发现(NSFetchedResultsController *)fetchedResultsController
定义了这些部分的组织方式,现在的问题是如何在这里生成我的自定义数组。
我的(NSFetchedResultsController *)fetchedResultsController
样子是这样的:
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Task" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"scheduledDate" ascending:YES];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _fetchedResultsController;
}
更新:生成的代码shortTerm
,midTerm
和longTerm
日期根据注意:我为此做[NSDate date]
了sectionIdentifier
很多NSDate
和NSString
类别。
-(NSArray *)shortTermDates {
NSArray *fullDateList = [NSDate daysFrom:[NSDate date] to:[NSDate dateWithDaysFromNow:2]];
NSMutableArray *daysList = [NSMutableArray array];
NSString *dateString = nil;
for (NSDate *date in fullDateList) {
dateString = [NSString dateStringFromFullDate:date];
[daysList addObject:dateString];
}
return fullDateList;
}
-(NSArray *)shortTermDateStrings {
NSMutableArray *daysList = [NSMutableArray array];
NSString *dateString = nil;
for (NSDate *date in [self shortTermDates]) {
dateString = [NSString dateStringFromFullDate:date];
[daysList addObject:dateString];
}
return daysList;
}
-(NSArray *)midTermDates {
NSDate *startDate = [[self shortTermDates] lastObject];
int addition = 5;
int dayFromLastTermDate = [NSDate extractDayFromDate:[[self shortTermDates] lastObject]];
NSDate *date = [NSDate dateWithYear:[NSDate extractYearFromDate:startDate] month:[NSDate extractMonthFromDate:startDate] day:dayFromLastTermDate+addition hour:[NSDate extractHourFromDate:startDate] minute:[NSDate extractMinuteFromDate:startDate] second:[NSDate extractSecondFromDate:startDate]];
NSArray *fullDateList = [NSDate daysFrom:startDate to:date];
return fullDateList;
}
-(NSArray *)midTermDateStrings {
NSMutableArray *daysList = [NSMutableArray array];
NSString *dateString = nil;
for (NSDate *date in [self midTermDates]) {
dateString = [NSString dateStringFromFullDate:date];
[daysList addObject:dateString];
}
return daysList;
}
-(NSArray *)longTermDates {
NSDate *startDate = [[self midTermDates] lastObject];
int addition = 8;
int dayFromLastTermDate = [NSDate extractDayFromDate:[[self midTermDates] lastObject]];
NSDate *date = [NSDate dateWithYear:[NSDate extractYearFromDate:startDate] month:[NSDate extractMonthFromDate:startDate] day:dayFromLastTermDate+addition hour:[NSDate extractHourFromDate:startDate] minute:[NSDate extractMinuteFromDate:startDate] second:[NSDate extractSecondFromDate:startDate]];
NSArray *fullDateList = [NSDate daysFrom:startDate to:date];
return fullDateList;
}
-(NSArray *)longTermDateStrings {
NSMutableArray *daysList = [NSMutableArray array];
NSString *dateString = nil;
for (NSDate *date in [self longTermDates]) {
dateString = [NSString dateStringFromFullDate:date];
[daysList addObject:dateString];
}
return daysList;
}
- (NSString *)sectionIdentifier;
{
NSDate *date = self.scheduledDate;
if ([date compare:[[self shortTermDates] lastObject]] == NSOrderedAscending) {
return @"0";
if ([date compare:[[self midTermDates] lastObject]] == NSOrderedAscending) {
return @"1";
} else {
return @"2";
}
}
return nil;
}