我希望我NSFetchRequest
的{{1}}根据特定属性对类似的记录(实体)进行分组。我目前正在执行两个步骤,但我相信可能会使用UITableViewController
。
有人可以帮助使用相应的代码吗?
这是我的两步过程的代码,它返回一个数组的数组:
+ (NSExpression *)expressionForAggregate:(NSArray *)collection
答案 0 :(得分:0)
你可以使用获取结果控制器为你做这样的切片:
NSFetchedResultsController* controller = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:context
sectionNameKeyPath:@"runClass"
cacheName:nil];
NSMutableArray* sections = [[NSMutableArray alloc] initWithCapacity:[controller sections] count];
for (id<NSFetchedResultsSectionInfo> section in [controller sections]) {
NSMutableArray* sectionCopy = [NSMutableArray arrayWithArray:[section objects]];
[sections addObject:sectionCopy];
}
或者自己动手:(假设结果按runClass
排序)
NSMutableArray* sections = [NSMutableArray new];
NSMutableArray* currentSection = [NSMutableArray new];
for (Run* run in dataArray) {
Run* lastObject = (Run*)[currentSection lastObject];
if (lastObject && (run.runClass == lastObject.runClass)) {
currentSection = [NSMutableArray new];
[sections addObject:currentSection];
}
[currentSection addObject:run];
}