Reputation: 2559
I know there are already questions out there but I don't understand them or I just do not know what I am doing. So I am getting this error when I try to run my app
Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier DateCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
I do not understand why I am getting this or how to resolve it.
I have a cell class which I have registered
import Foundation
import UIKit
class CVCell: UICollectionViewCell {
@IBOutlet weak var myCellLabel: UILabel!
}
And then here is my viewController:
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
private let reuseIdentifier = "DateCell"
override func viewDidLoad() {
initializeVars()
//moreDateInfo()
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return moreDateInfo()
}
// make a cell for each cell index path
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
// get a reference to our storyboard cell
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CVCell
// Use the outlet in our custom class to get a reference to the UILabel in the cell
cell.myCellLabel.text = String(startOfMonthDate)
if (startOfMonthDate == numOfDaysInThisMonth) {
startOfMonthDate = 1
} else {
startOfMonthDate++
}
//cell.backgroundColor = UIColor.yellowColor() // make cell more visible in our example project
cell.layer.borderColor = UIColor.blackColor().CGColor
cell.layer.borderWidth = 1
cell.layer.cornerRadius = 10
return cell
}
// MARK: - UICollectionViewDelegate protocol
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
// handle tap events
print("You selected cell #\(indexPath.item)!")
}
func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.cellForItemAtIndexPath(indexPath)
cell?.backgroundColor = UIColor.blackColor()
}
// change background color back when user releases touch
func collectionView(collectionView: UICollectionView, didUnhighlightItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.cellForItemAtIndexPath(indexPath)
cell?.backgroundColor = UIColor.whiteColor()
}
Upvotes: 0
Views: 2746
Reputation: 1798
On my storyboards with prototype cells, it is not required to register the class. The storyboard takes care of that.
Upvotes: 1
Reputation: 712
This line:
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CVCell
does not actually register your CVCell class. To do that, you should use registerClass:forCellReuseIdentifier:
in your viewDidLoad
. Alternatively, in your storyboard select the cell, and in property inspector on the right, enter "DataCell' under "reuse identifier".
Upvotes: 1
Reputation: 499
Your cell name is wrong its "DateCell"
And may be you need to register the nib class like below
self.collectionViewCategory.registerNib(UINib(nibName: "SelectCategoriesCell", bundle: nil), forCellWithReuseIdentifier: reuseIdentifier)
Upvotes: 1
Reputation: 2559
Wow I had a spelling error.....never forget those typos.
private let reuseIdentifier = "DateCell"
Should be "DataCell"
Upvotes: 0