对于分组的 UITableView 中的部分,我有一个 NSManagedObject。该对象具有属性“name”和“createdAt”。我想在 te UI 中使用“名称”作为部分标题,但按“createdAt”排序。根据文档,第一个 sortDescriptor 键也必须是 NSFetchedResultsController 的 sectionNameKeyPath。
我建议使用两个 sortDescriptor,但它不起作用。这些部分仍按名称排序。
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Object" inManagedObjectContext:[CoreDataHelper instance].managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortName = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSSortDescriptor *sortDate = [[NSSortDescriptor alloc] initWithKey:@"createdAt" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortName, sortDate, nil]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:[CoreDataHelper instance].managedObjectContext sectionNameKeyPath:@"name"
cacheName:@"Root"];
self.fetchedResultsController = theFetchedResultsController;
return _fetchedResultsController;
}