Unable to call UICollectionReusableView method

I want my sections in a UICollectionView have a caption with an image.

I have completed the following steps:

  • on the storyboard, assigned a title as an accessory for my UICollectionView
  • gave him an identifier
  • created a subclass of UICollectionReusableView for it
  • assigned a custom class to the storyboard class.
  • type ImageView in assembler header
  • made an exit for ImageView in a user class in a .h file
  • The viewDidLoad implemented in viewDidLoad :

 [self.collectionView registerClass:[ScheduleHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier]; -(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { UICollectionReusableView *reusableview = nil; if (kind == UICollectionElementKindSectionHeader) { ScheduleHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier forIndexPath:indexPath]; headerView.headerImageView.image = [UIImage imageNamed:@"blah.png"]; reusableview = headerView; } return reusableview; } 

I know that the datasource and delegate methods work because I can see all the cells and their sections. However, I have no headlines. I set a breakpoint using the method described above and have never been called.

What am I doing wrong?

+60
ios uicollectionview
Aug 11 '13 at 14:54
source share
9 answers

It seems that you need to give your header a nonzero size or collectionView:viewForSupplementaryElementOfKind:atIndexPath not called. Therefore, either set the layout of the stream for the headerReferenceSize property headerReferenceSize this:

 flowLayout.headerReferenceSize = CGSizeMake(self.collectionView.frame.size.width, 100.f); 

or, implement collectionView:layout:referenceSizeForHeaderInSection if you want to resize by sections.

+166
Aug 11 '13 at 16:41
source share

You answered the question, but in words I understand better:

To call the called method, add the following methods:

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { return CGSizeMake(60.0f, 30.0f); } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section { return CGSizeMake(60.0f, 30.0f); } 

... and go from there.

+29
Jun 18 '14 at 22:19
source share

Swift 4 Xcode 9.1 Beta 2

Add @objc

 @objc func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize{ let size = CGSize(width: 400, height: 50) return size } 

Credits: https://medium.com/@zonble/your-delegation-methods-might-not-be-called-in-swift-3-c6065ed7b4cd

+18
Oct 12 '17 at 21:44
source share

Have you added UICollectionViewDelegateFlowLayout to the current interface? It worked for me.

@interface MyViewController() <UICollectionViewDelegateFlowLayout>

Swift:

class MyViewController: UICollectionViewDelegateFlowLayout {

+10
Jan 17 '15 at 4:13
source share

Swift 3.0 (xcode 8.2.1)

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { return CGSize(width:collectionView.frame.size.width, height:30.0) } 
+7
Jan 17 '17 at 18:54
source share

There is a quick version:

 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { let size = CGSize(width: 400, height: 50) return size } func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize { let size = CGSize(width: 400, height: 50) return size } 

Xcode (version 7.3.1) does not offer these methods in autocomplete!

If you just want a title, just save the title method.

+5
Aug 15 '16 at 12:57
source share

My problem was that in swift 3, the name of the viewForSupplementaryElementOfKind function changed slightly from any messages that were on the stack overflow. Therefore, he was never called. Make sure that if you are in fast 3 mode, you are acting as a delegate:

 func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {} 
+2
Feb 18 '17 at 16:38
source share

I have the same problem. I added referenceSizeForFooterInSection but then also not viewForSupplementaryElementOfKind . I added this code to viewDidLoad() and it worked for me:

  if let flowLayout = self.collectionView.collectionViewLayout as? UICollectionViewFlowLayout { flowLayout.sectionFootersPinToVisibleBounds = true } 
+1
Jan 31 '19 at 13:17
source share

For me, in Swift 4.2, func collectionView (collectionView: kind: indexPath :) -> UICollectionReusableView has never been called. This was to represent the collection in the UIViewController. I noticed that the existing collection view functions were qualified using @objc, and that UICollectionView did not accept the UICollectionViewDataSource and UICollectionViewDelegate protocols. As soon as I accepted the protocols, I got errors stating that the collection view functions are not protocol compliant. I fixed the function syntax, removed the classifiers, and the section headers started working.

0
May 7 '19 at 12:38
source share



All Articles