UICollectionView footer

I am trying to add a footer to a UICollectionView.

Below is my code,

UICollectionView is added via IB

In viewDidLoad I will register a footer,

[mCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"]; 

And the following method is implemented

 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { UICollectionReusableView *reusableview = nil; if (kind == UICollectionElementKindSectionFooter) { UICollectionReusableView *headerView = [mCollectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"footer" forIndexPath:indexPath]; [headerView addSubview:mFooterView]; reusableview = headerView; } return reusableview; } 

But my application continues to crash, and below is the log,

*** Validation error in - [UICollectionView _dequeueReusableViewOfKind: withIdentifier: forIndexPath:], / SourceCache / UIKit / UIKit-2380.17 / UICollectionView.m: 2249

Any help is appreciated.

Thanks.

+6
source share
2 answers

in your code, why are you looking at the header and adding a footer to it?

normal implementation of this method:

 - (UICollectionReusableView *)collectionView:(UICollectionView *)theCollectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)theIndexPath { UICollectionReusableView *theView; if(kind == UICollectionElementKindSectionHeader) { theView = [theCollectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:theIndexPath]; } else { theView = [theCollectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:theIndexPath]; } return theView; } 
+13
source

For Swift 4

  override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { var myView = UICollectionReusableView() if kind == UICollectionView.elementKindSectionHeader { myView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: myHeader, for: indexPath) } else { myView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: myFooter, for: indexPath) } return myView } 
0
source

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


All Articles