Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: UICollectionView Footer not showing

My UICollectionView's footer is not displaying any of its subviews. I've looked around for answers, but nothing seems to be working in my case. I'm not sure if it's something in Swift or I'm just missing something.

In my storyboard, I've checked the Section Footer box and added a few buttons to the footer's view. I've set the reusable identifier to 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 up. However, when I run the app, the footer does not show any subviews (footer.subviews.count = 0). The footer's frame is correct, but why are the subviews I put in the storyboard not showing up?

Here's 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
}
like image 363
Maxwell Avatar asked Aug 22 '14 15:08

Maxwell


2 Answers

Try removing the registerClass line:

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

registerClass is only used if you are creating the view/cell programmatically. If you've created it in the storyboard, then registerClass will overwrite the one from the storyboard.

like image 63
Ron Fessler Avatar answered Nov 03 '22 16:11

Ron Fessler


Using registerNib instead of registerClass worked for me,

    IBcollectionView.registerNib(UINib(nibName: id, bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: id)
  • So when calling registerClass it just create new object from that class only, and doesn't load nib's view, but calling registerNib does the trick.
like image 2
Mohammad Zaid Pathan Avatar answered Nov 03 '22 14:11

Mohammad Zaid Pathan