在表视图部分中排列核心数据对象

我正在使用NSSortDescriptors来订购核心数据对象,并根据date属性创build表视图部分。 使用这种types的SortDescriptor,表视图部分按预期sorting,但部分内的行也由相同的date属性sorting。 有没有办法在每个部分有另一个订购系统? 我猜主要的问题是核心数据存储date对象与date+时间值,这样就没有对象具有完全相同的date值。 但是我需要根据另一个属性来排列同一部分中的对象。 谢谢。

这里是我的NSFetchedResultsController的代码:

-(NSFetchedResultsController*)fetchedResultsController{ if (_fetchedResultsController != nil){ return _fetchedResultsController; } NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init]; NSManagedObjectContext *context = self.managedObjectContext; NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:context]; [fetchRequest setEntity:entity]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"itemDate" ascending:YES]; NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc]initWithKey:@"itemName" ascending:NO]; NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor,sortDescriptor1, nil]; fetchRequest.sortDescriptors = sortDescriptors; _fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:@"sectionIdentifier" cacheName:nil]; _fetchedResultsController.delegate = self; return _fetchedResultsController; } 

编辑的问题

这是添加到cellForRowAtIndexPath方法的代码片段:

 int indexpathsection = indexPath.section; [self sortedSectionForIndex:indexpathsection]; NSLog(@"INDEXPATH= %ld", (long)indexPath.section); 

这是马库斯提出的方法:

 - (NSArray*)sortedSectionForIndex:(NSInteger)index { NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"priority" ascending:YES]; id section = [[self fetchedResultsController] sections][index]; NSLog(@"INDEX********* = %ld", (long)index); NSArray *objects = [section objects]; NSArray *sorted = [objects sortedArrayUsingDescriptors:@[sort]]; return sorted; } 

NSFetchedResultsController是为了通过一个sorting或一组sorting来控制sorting顺序。 它并不打算做你正在做的事情。

但是你可以做到这一点,而不是干净。

首先,讨论sorting一节。 这并不要求你编写你自己的sortingalgorithm:

 - (NSArray*)sortedSectionForIndex:(NSInteger)index { NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"MyKey" ascending:YES]; id section = [[self fetchedResultsController] sections][index]; NSArray *objects = [section objects]; NSArray *sorted = [objects sortedArrayUsingDescriptors:@[sort]]; return sorted } 

用这种方法,你可以要求一个部分进行sorting,只要你想检索一个对象。 然而,这是低效率的,因为你正在sorting每次你想触摸一个UITableViewController是一个很多的对象。

相反,采取这个例子,并将其与NSFetchedResultsController及其委托方法集成,以便您存储sorting的数组并保持同步。 这样,只有在数据发生变化而不是每次方法调用时才进行sorting。

更新

我提供给您的sorting代码不会对NSFetchedResultsControllersorting。 它需要在该部分中进行sorting,并返回sorting后的数组。 所以你需要使用它返回的内容:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSArray *sorted = [self sortedSectionForIndex:[indexPath section]]; id object = [sorted objectAtIndex:[indexPath row]]; id cell = ...; //Now get your cell reference //Now update your cell with the data from object return cell; } 

你可以在你的NSManagedObjectawakeFromFetch方法中规范你的属性的时间组件。

 - (void) awakeFromFetch { [super awakeFromFetch]; NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:self.itemDate]; self.itemDate = [[NSCalendar currentCalendar] dateFromComponents:comps]; } 

另外,看看你的代码,你必须告诉NSFetchedResultsController它应该以什么为基础的分节符。 您可以通过在初始化时为分节符提供keyPath来完成此操作。 对于你的情况,而不是使用sectionNameKeyPath:@"sectionIdentifier" ,使用第一个sorting键:

  _fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:@"itemDate" cacheName:nil];