Domanda

I am getting the following error implementing the supplementary Header View

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no UICollectionViewLayoutAttributes instance for -layoutAttributesForSupplementaryElementOfKind: HeaderView at path <NSIndexPath: 0x9e82a40> {length = 2, path = 0 - 0}'

This is the code that is creating header view

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    static NSString * headerIdentifier = @"HeaderView";
    UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:headerIdentifier withReuseIdentifier:UICollectionElementKindSectionHeader forIndexPath:indexPath];
    return header;
}

The error happens in dequeueReusableSupplementaryViewOfKind method.

I have added these two lines in my initWithCoder of the Collection View Controller

UINib *headerNib = [UINib nibWithNibName:@"MTCollectionHeaderView" bundle:nil];
[self.collectionView registerNib:headerNib forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];

so it does have a reference to the UI element. I cannot find anyway to set the layout on the header.

I tried adding this to the same Collection View Controller but it never hit this code as I confirmed by putting a debugger there

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    return [self.collectionViewLayout layoutAttributesForDecorationViewOfKind:kind atIndexPath:indexPath];
}

Has anyone seen this issue and how did they resolve it. Also I am using XCode 5 Developer Preview 5 and developing for iOS7

È stato utile?

Soluzione

I believe you are passing the headerIdentifier for the kind of supplementary view and the kind constant for the header identifier. Try switching them as shown in this line of code in your collectionView:viewForSupplementaryElementOfKind:atIndexPath: method:

UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier forIndexPath:indexPath];

I also do not think you need your implementation of layoutAttributesForSupplementaryElementOfKind:atIndexPath:, swap the above line of code in and see if it works.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top