H4Hugo
H4Hugo

Reputation: 2650

UICollectionViewDelegateFlowLayout makes section header disappear

I created a simple collection view and added dummy data (see code below)

Everything was working well until I added the CollectionViewDelegateFlowLayout, it made my section header disappear. Any idea why this is happening and how I can have both my section headers and a custom layout ?

Code:

import UIKit

private let reuseIdentifier = "Cell"

class HomeCollectionViewController: UICollectionViewController {

var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"]

override func viewDidLoad() {
    super.viewDidLoad()

    // Setup a layout
    setupCollectionViewLayout(minimumInteritemSpacing: 0, minimumLineSpacing: 0)
}


// MARK: UICollectionViewDataSource

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 2
}


override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return items.count
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyCollectionViewCell

    // Configure the cell
    cell.myLabel.text = items[indexPath.item]

    return cell
}

override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
    let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath) as! MyCollectionReusableView

    headerView.sectionLabel.text = "Toto"

    return headerView

}

func setupCollectionViewLayout(minimumInteritemSpacing minimumInteritemSpacing: CGFloat, minimumLineSpacing: CGFloat) {
    let layout = UICollectionViewFlowLayout()
    layout.minimumInteritemSpacing = minimumInteritemSpacing
    layout.minimumLineSpacing = minimumLineSpacing
    collectionView?.collectionViewLayout = layout
}

}

// ----------------------------------------------------------------------------------
// MARK: - UICollectionViewDelegateFlowLayout
// ----------------------------------------------------------------------------------

extension HomeCollectionViewController: UICollectionViewDelegateFlowLayout {

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

    switch UIDevice.currentDevice().userInterfaceIdiom {
    case .Phone:
        return CGSize(width: self.view.frame.width, height: 100.0)
    case .Pad:
        return CGSize(width: self.view.frame.width/2.0, height: 100.0)
    default:
        return CGSize(width: self.view.frame.width, height: 100.0)
    }


}
}

Upvotes: 0

Views: 1040

Answers (1)

H4Hugo
H4Hugo

Reputation: 2650

I managed to do it thanks to @avismara's advice.

When using a custom layout you must specify a size on your layout like so:

// Supposing you have created a layout, add:
layout.headerReferenceSize = CGSizeMake(HEADER_WIDTH, HEADER_HEIGHT)

Upvotes: 1

Related Questions