IOS custom collectionView with custom headers

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") // UICollectionReusableView self.view.addSubview(collectionView) } 

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 // 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: "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") } } 
+11
ios swift subclass uicollectionview
source share
4 answers

So I realized this, inspired by Mohamad Farhand.

The problem was that I had to register the subclass itself with collectionView, and not UICollectionReusableView.self , I used an 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 
+15
source share

Note that Swift 4.1 renames the constant ofKind: to UICollectionView.elementKindSectionHeader .

+2
source share

you can do it like this:

  // Setup Header self.collectionView?.registerClass(CollectionCustomHeader.self, forSupplementaryViewOfKind: CustomeHeaderHeader, withReuseIdentifier: "customHeader") 

and

  override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { if kind == CustomeHeaderHeader { let view = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "parallaxHeader", forIndexPath: indexPath) return view } 
+1
source share

Here is the Swift 3 & 4 answer I used in the project

 self.collectionView.register(LibraryHeaderNib.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader , withReuseIdentifier: "LibraryHeaderNib") 

and inside viewForSupplementaryElementOfKind

 let reusableView = self.collectionView!.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "LibraryHeaderNib", for: indexPath) as! LibraryHeaderNib return reusableView 
0
source share

All Articles