I am making the iOS application fast and I am trying to make collectionView programmatically. I want to use my own subclass of UICollectionReusableView as the title for the collection, because I need buttons and an extensible image in the title.
SupView is a 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")
I am trying to insert an additional view into viewForSupplementaryElementOfKind like this, but I get an error when trying to create a header:
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { var reusableView : UICollectionReusableView? = nil
The error is in let headerView = ... and says: "SIGABRT signal"
How do I initialize a headerview so that I can contribute to my stream? possibly using
collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell")
but if I try to register the SupView class, it will give me an error:
... / collectionViewPlay / ViewController.swift: 32: 24: Cannot call 'registerClass' with a list of arguments of type '(SupView !, forSupplementaryViewOfKind: String, withReuseIdentifier: String)'
Any ideas?
EDIT:
A subclass implementation 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") } }