gpt4 book ai didi

ios - NSFetchedResultsController 与项目在多个部分?

转载 作者:行者123 更新时间:2023-11-28 20:22:59 25 4
gpt4 key购买 nike

我有一个托管对象来表示 map 上的一个点。这些点有零多种类型。我还想显示一个分区 TableView 。当 type 是 MapPOI 对象上的单个值时,我使用了 NSFetchedResultsController。但是现在类型在不同的对象中,“MapPOI”之间的关系称为“类型”,我该如何编写查询(可以吗?)

原文:

- (NSFetchedResultsController *)newFetchedResultsControllerWithSearch:(NSString *)searchString
{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"MapPOI"];

if(searchString.length)
{
request.predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@", searchString];
}
request.sortDescriptors = [NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:@"type" ascending:YES ],[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES ],nil ];

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.campus.managedObjectContext
sectionNameKeyPath:@"type"
cacheName:nil];
aFetchedResultsController.delegate = self;


NSError *error = nil;
if (![aFetchedResultsController performFetch:&error])
{
NSLog(@"Error performing institution fetch with search string %@: %@, %@", searchString, error, [error userInfo]);
}

return aFetchedResultsController;
}

我试过类似的东西

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.campus.managedObjectContext
sectionNameKeyPath:@"types.name"
cacheName:nil];

但是那导致了

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid to many relationship in setPropertiesToFetch: (types.name)'

最佳答案

NSFetchedResultsController 非常灵活,可以用于各种 View ,而不仅仅是 TableView 。

sectionNameKeyPath 当然是错误的。它显然必须是一对一的关系。假设一个MapPOI只能有一种类型,关键路径应该是类似@"type.name"的东西。

但是,如果一个 MapPOI 可以有多种类型,您可以执行以下操作:

获取类型实体而不是 POI 实体。您不需要节键路径。现在 objectAtIndexPath:indexPath.row 将获取一个 Type 托管对象。

对于节数使用

self.fetchedResultsController.fetchedObjects.count

部分标题使用

[[self.fetchedResultsController.fetchedObjects objectAtIndex:section] name];

用于部分使用中的行数

Type *type = [self.fetchedResultsController.fetchedObjects objectAtIndex:section];
type.mapPOIs.count;

而且如何使用 MapPOI 实体填充单元格应该是显而易见的。

关于ios - NSFetchedResultsController 与项目在多个部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15228456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com