从UICollectionView的viewForSupplementaryElementOfKind获取NSFetchedResultsController节对象

我有一个名为Location的数据模型。 我在UICollectionViewController使用这些作为我的部分标题。 每个location都可以显示这些部分内的items 。 我想在viewForSupplementaryElementOfKind自定义我的部分标题。 但我不知道如何从这个方法recive正确的Location对象。

我曾尝试过这样的事情:

 -(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' 

这可能是因为可能没有任何项目的位置。 任何其他的想法,我应该怎么做?

您的代码看起来非常正确,并且在我的小testing应用程序中按预期工作。 因此我假设你在数据源方法中有一些错误。

这是我用过的:

 -(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; } 

获取的结果控制器创build如下:

 - (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; } 

备注:采集结果控制器自动更新集合视图是非常棘手的,所以这篇文章

http://ashfurrow.com/blog/how-to-use-nsfetchedresultscontroller-with-uicollectionview

(带有代码示例)可能很有趣。