2

I'm using RxSwift, I managed to create the dataSource and I retrieve cells correctly. Problem is with the section headerView. I have created a UICollectionReusableView class, attaching outlets from storyboard.

The problem is that I retrieve the view with collectionview.dequeueReusableSupplementaryView, but awakeFromNib is never called!

This is how I setup the collectionView

collectionView.register(UINib(nibName: "CardView", bundle: Bundle.main), forCellWithReuseIdentifier: "cardView")
collectionView.register(AddNewCardCollectionHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "AddNewCardCollectionHeaderView")

this is my UICollectionReusableView class

class AddNewCardCollectionHeaderView: UICollectionReusableView {

    @IBOutlet weak var collectionViewRecommendations: UICollectionView!
    @IBOutlet weak var viewWrapperRecommendations: UIView!

    override func awakeFromNib() {
        super.awakeFromNib()

    }

}

And this is the dataSource

let dataSource = RxCollectionViewSectionedReloadDataSource<SectionOfItems>(configureCell: { (datasource, collectionview, indexPath, i) -> UICollectionViewCell in
            let cell = collectionview.dequeueReusableCell(withReuseIdentifier: "cardView", for: indexPath) as! CardView
            let card = self.getCard(atRow: indexPath.row, isCardRecommendation: false)
            self.setCell(card:card,cell:cell)
            return cell
        }, configureSupplementaryView: { (datasource, collectionview, kind, indexPath) -> UICollectionReusableView in
            let headerView = collectionview.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "AddNewCardCollectionHeaderView", for: indexPath) as! AddNewCardCollectionHeaderView
            return headerView
        })

As awakeFromNib is not called, if I do for example headerView. viewWrapperRecommendations it will crash as nil, but I need to access to headerView outlets.

But, instead, awakeFromNib of CardView (the cell class) is called and it works perfectly.

Any help? Thanks!

Andrea.Ferrando
  • 987
  • 13
  • 23
  • Unrelated to RxSwift ... If `AddNewCardCollectionHeaderView` doesn't come from a nib, why would `awakeFromNib` be called ? – Valérian Feb 02 '18 at 13:24
  • It comes from storyboard, inside the view controller, but the outlets are nil – Andrea.Ferrando Feb 02 '18 at 16:04
  • I'm not familiar with UICollectionView but seeing how you registered `AddNewCardCollectionHeaderView`, I think it won't load from the `storyboard`. You either register the one type of header you have directly in the `storyboard` or you have to have a `nib` per header class (in which case you use the following method [register(_:forSupplementaryViewOfKind:withReuseIdentifier:)](https://developer.apple.com/documentation/uikit/uicollectionview/1618101-register)) – Valérian Feb 02 '18 at 17:09
  • @Andrea.Ferrando are you sure the hight of your header view is higher than 0? Try hardcoding it. – Timofey Solonin Feb 04 '18 at 12:11

0 Answers0