Reputation: 532
Here is my override function for UICollectionUICollectionViewControllerView
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
var view = UICollectionReusableView()
if kind == UICollectionElementKindSectionHeader {
let viewHeader : Header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "header", forIndexPath: indexPath) as! Header
if let userName = viewHeader.userName {
userName.text = "James Bond"
}
view = viewHeader
}
return view
}
The problem is whenever I tried to run the app, following the debugging it always popup error "fatal error: unexpectedly found nil while unwrapping an Optional value" in the line
let viewHeader : Header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "header", forIndexPath: indexPath) as! Header
Below is my viewDidLoad override
override func viewDidLoad() {
super.viewDidLoad()
let layout = UICollectionViewFlowLayout()
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
self.collectionView!.delegate = self
collectionView!.dataSource = self
collectionView!.registerClass(SquareCell.self, forCellWithReuseIdentifier: "cell")
collectionView!.registerClass(Header.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header") // UICollectionReusableView
layout.headerReferenceSize = CGSizeMake(self.view.frame.width, 200)
let cellNib = UINib(nibName: "SquareCell", bundle: nil)
collectionView!.registerNib(cellNib, forCellWithReuseIdentifier: "cell")
let headerlNib = UINib(nibName: "HeaderView", bundle: nil)
collectionView!.registerNib(headerlNib, forCellWithReuseIdentifier: "header")
self.view.addSubview(collectionView!)
}
Upvotes: 0
Views: 768
Reputation: 532
I found out that in my Header.swift class I accidentally add non-existing image name
override init(frame: CGRect) {
super.init(frame: frame)
self.cover.image = UIImage(named: "cover")
self.userName.text = "Jame Bond"
}
so I deleted the image name and all back to normal :)
Upvotes: 1