“Photo”实体(anAlbum.photos是关系)包含 资产网址。我对显示没有任何问题,它更像是关于如何使用NSSet(核心数据关系)与 NSFectchedResultsController - 或直接与视图 (集合/表格)关联的 。
首先,我会用这个NSFetchedResultsController
。该组件与表结合使用,并允许以延迟加载的方式加载数据。 其次,我将使用的提取请求应针对Photo
实体运行,而不是针对Album
运行。换句话说,您应该选择属于特定专辑的所有照片。
这里的代码,我会用...
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Photo" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"album == %@", anAlbum];
[request setPredicate:predicate];
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:@"takeAt" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext sectionNameKeyPath:nil
cacheName:nil];
self.fetchedResultsController = theFetchedResultsController;
return _fetchedResultsController;
}
所以现在你tableView:cellForRowAtIndexPath:
你可以使用
Photo *photo = [_fetchedResultsController objectAtIndexPath:indexPath];
// access the album the photo belongs to
Album* album = photo.album;
您可以在Core Data Tutorial for iOS: How To Use NSFetchedResultsController把握NSFetchedResultsController
的主要概念。
你想展示什么?照片或相册? –
“照片”实体(anAlbum.photos是关系)包含资产网址。我对显示没有任何问题,我更关心如何使用NSFectchedResultsController的NSSet(核心数据关系) - 或直接使用视图(collection/table)。 – chrisp