Programmatically create UICollectionView with custom headers

Wiingaard picture Wiingaard · Oct 26, 2015 · Viewed 39k times · Source

I'm making an iOS app in swift, and I'm trying to make a collectionView programmatically. I want to use my own subclass of UICollectionReusableView as a header for the CollectionView, because I need some buttons and a stretchable image in the header.

SupView is the UICollectionReusableView.

override func viewDidLoad() {
    super.viewDidLoad()


    let layout = UICollectionViewFlowLayout()
    layout.headerReferenceSize = CGSizeMake(self.view.frame.width, 200)

    someView = SupView(frame: CGRectMake(0, 0, view.frame.width, 200))

    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    collectionView.delegate = self
    collectionView.dataSource = self

    collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
    collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell")  // UICollectionReusableView
    self.view.addSubview(collectionView)
}

I'm trying to insert the Supplementary View in viewForSupplementaryElementOfKind, like this but I'm getting an error when trying to create the header:

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
    var reusableView : UICollectionReusableView? = nil

    // Create header
    if (kind == UICollectionElementKindSectionHeader) {
        // Create Header
        let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell", forIndexPath: indexPath) as! SupView
        headerView.frame = CGRectMake(0, 0, view.frame.width, 200)

        reusableView = headerView
    }
    return reusableView!
}

The error is in let headerView = ... and says: "signal SIGABRT"

How should I initialize the headerview, so the I can input to my flowlayout?

maybe somewith with

collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell")

but if I try to register the SupView-class it gives me error:

.../collectionViewPlay/ViewController.swift:32:24: Cannot invoke 'registerClass' with an argument list of type '(SupView!, forSupplementaryViewOfKind: String, withReuseIdentifier: String)'

Any Ideas?

EDIT:

The implementation of the subclass was requested:

import UIKit

class SupView: UICollectionReusableView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.myCustomInit()
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        self.myCustomInit()
    }

    func myCustomInit() {
        print("hello there from SupView")
    }

}

Answer

Wiingaard picture Wiingaard · Oct 26, 2015

So I figured it out, with inspiration from Mohamad Farhand.

The problem was that I had to register the subclass itself with the collectionView, instead of UICollectionReusableView.self, I used the instance of the subclass someView.. So this solved my problem:

collectionView.registerClass(SupView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader , withReuseIdentifier: "someRandonIdentifierString")

And how to initialize the view:

someView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "someRandonIdentifierString", forIndexPath: indexPath) as! SupView