Error adding extra header to UICollectionView

I get the following error implementing an optional header

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 creates the 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 occurs in the dequeueReusableSupplementaryViewOfKind method.

I added these two lines to my initWithCoder collection controller.

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

therefore, it has a link to a user interface element. I just can't find the layout in the title.

I tried to add this to the same Control View Controller, but I never hit this code, as I confirmed by setting the debugger there

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

Has anyone seen this problem and how they solved it. I also use Xcode 5 Developer Preview 5 and develop for iOS7

+4
source share
1 answer

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

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

I also don't think you need your implementation of layoutAttributesForSupplementaryElementOfKind:atIndexPath: replace the line of code above and see if it works.

+6
source

Source: https://habr.com/ru/post/1496078/


All Articles