从UICollectionView的viewForSupplementaryElementOfKind获取NSFetchedResultsController节对象 [英] Get NSFetchedResultsController section object from UICollectionView's viewForSupplementaryElementOfKind
问题描述
我有一个名为 Location
的数据模型。我使用这些作为我的节头在 UICollectionViewController
。每个位置
可以在这些部分中显示项目
。我想在 viewForSupplementaryElementOfKind
中自定义我的部分标题。但我不知道如何从这个方法中回收正确的 Location
对象。
I have a data model called Location
. I use these as my section headers in a UICollectionViewController
. Each location
can display items
inside these sections. I want to customise my section headers in viewForSupplementaryElementOfKind
. But I can't figure out how to recive the correct Location
objects from this method.
例如:
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections] [indexPath.section];
Item *item = sectionInfo.objects[0];
Location *location = item.location;
SectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SectionHeader" forIndexPath:indexPath];
headerView.label.text = location.name;
[...]
但我得到:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
这可能是因为它可能没有任何项目的位置。关于我应该如何做的任何其他想法?
This is probably because it's possible to have Location without any items. Any other ideas on how I should do it?
推荐答案
您的代码看起来很正确, app。
因此,我假设你在数据源方法中有一些错误。
Your code looks quite correct, and it worked as expected in my small test app. Therefore I assume that you have some error in the data source methods.
这是我使用的:
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return [[self.frc sections] count];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [self.frc sections][section];
return [sectionInfo numberOfObjects];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CellView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellView" forIndexPath:indexPath];
Item *item = [self.frc objectAtIndexPath:indexPath];
cell.name.text = item.name;
return cell;
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
id <NSFetchedResultsSectionInfo> sectionInfo = [self.frc sections][indexPath.section];
Item *item = sectionInfo.objects[0];
Location *location = item.location;
HeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
headerView.title.text = location.name;
return headerView;
}
获取的结果控制器创建如下:
The fetched results controller is created as follows:
- (NSFetchedResultsController *)frc
{
if (_frc == nil ) {
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Item"];
NSSortDescriptor *sort1 = [[NSSortDescriptor alloc] initWithKey:@"location.name" ascending:NO];
NSSortDescriptor *sort2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
NSArray *sortDescriptors = @[sort1, sort2];
[fetchRequest setSortDescriptors:sortDescriptors];
_frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.context
sectionNameKeyPath:@"location.name"
cacheName:nil];
NSError *error;
_frc.delegate = self;
[_frc performFetch:&error];
}
return _frc;
}
备注: / em>与收集结果控制器
的集合视图相当棘手,因此本文
Remark: The automatic updates of a collection view with a fetched results controller are quite tricky, so this article
http://ashfurrow.com/blog/how-to-use-nsfetchedresultscontroller-with-uicollectionview
(有代码示例)可能很有趣。
(with code examples) might be interesting.
这篇关于从UICollectionView的viewForSupplementaryElementOfKind获取NSFetchedResultsController节对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!