4

我有一个名为Location. 我将这些用作UICollectionViewController. 每个都location可以显示items在这些部分内。我想在viewForSupplementaryElementOfKind. 但我不知道如何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'

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

4

1 回答 1

3

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

这是我使用的:

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

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

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

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

于 2013-10-26T17:06:17.377 回答