Get NSFetchedResultsController section object from

2019-06-28 03:01发布

问题:

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.

I have tried things like:

-(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;
    [...]

But I keep getting:

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?

回答1:

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.

This is what I used:

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

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.