Get NSFetchedResultsController section object from UICollectionView viewForSupplementaryElementOfKind

I have a data model called Location. I use them as section headers in UICollectionViewController. Everyone Locationcan display itemsinside these sections. I want to customize the section headers in viewForSupplementaryElementOfKind. But I cannot figure out how to extract the correct objects from this method Location.

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

Perhaps this is due to the fact that it is possible to place without any elements. Any other ideas on how I should do this?

+4
source share
1

, . , .

, :

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

( ) .

+3

All Articles