CollectionView footer not showing

I checked the section footer as shown in the screenshot below.

collectionViewAttributesInspector

I also implemented the dataSource method:

  override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { if kind == UICollectionElementKindSectionHeader { var header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath) as? DetailHeader if header == nil { header = DetailHeader() } return header! } else { println("footer") var footer = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Footer", forIndexPath: indexPath) as! UICollectionReusableView return footer } } 

I managed to display my header, but my footer is never displayed. println never starts.

+4
source share
3 answers
  • Screenshot from the storyboard: enter image description here

2. My code:

 import UIKit class BaseCVC: UICollectionViewController, UICollectionViewDelegateFlowLayout { override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cvcCell", forIndexPath: indexPath) return cell; } override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return Int(2) } func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize { return CGSizeMake(self.view.frame.size.width, 20) } override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { if kind == UICollectionElementKindSectionHeader { let header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "header", forIndexPath: indexPath) return header } else { print("footer") let footer = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "footer", forIndexPath: indexPath) return footer } } } 
  1. Result in the simulator:

enter image description here

  1. Important Note . I used Xcode 7 beta 5 (Swift 2.0).
+4
source

Option 1 : set footerReferenceSize to UICollectionViewDelegateFlowLayout greater than 0

Option 2 Add the collectionView(_:layout:referenceSizeForFooterInSection:) function collectionView(_:layout:referenceSizeForFooterInSection:) .

+2
source

Drag the CollectionReusableView for the header and footer into the UICollectionViewController from storyBoard and give an identifier for each. In the same process, you drag a UITableViewCell into a UITableView and specify the cell ID. Good luck.

-2
source

All Articles