Swift: UICollectionView footer not showing

My UICollectionView footer does not display any of its subzones. I was looking for answers, but in my case nothing works. I'm not sure if this is something in Swift, or if I just missed something.

In my storyboard, I checked the Footer box and added a few buttons to the footer. I set the reusable identifier to the "footer" and registered it in the view controller as a. Then I called collectionView(collectionView: UICollectionView!, viewForSupplementaryElementOfKind kind: String!, atIndexPath indexPath: NSIndexPath!) , Which sets it. However, when I launch the application, the footer does not show any subzones ( footer.subviews.count = 0). The footer border is correct, but why are the sub-items that I put in the storyboard not showing up?

Here is the code:

 override func viewDidLoad() { ... uiCollectionView.registerClass(MyFooterView.classForCoder(), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "footer") } func collectionView(collectionView: UICollectionView!, viewForSupplementaryElementOfKind kind: String!, atIndexPath indexPath: NSIndexPath!) -> UICollectionReusableView! { let reusableView:MyFooterView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionFooter, withReuseIdentifier: "footer", forIndexPath: indexPath) as MyFooterView println("Footer subivews: \(reusableView.subviews.count)") // 0 return reusableView } 
+5
source share
2 answers

Try removing the registerClass line:

 uiCollectionView.registerClass(MyFooterView.classForCoder(), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "footer") 

registerClass is used only if you create the view / cell programmatically. If you created it in the storyboard, then registerClass will overwrite one of the storyboards.

+1
source

Using registerNib instead of registerClass worked for me,

  IBcollectionView.registerNib(UINib(nibName: id, bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: id) 
  • Therefore, when calling registerClass it simply creates a new object only from this class and does not load the nib representation, but calling registerNib does the trick.
+1
source

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


All Articles