Could not cast value of type 'UICollectionViewCell'
The template for a CollectionViewController in Xcode comes with this line of code in viewDidLoad, which changes the specification in your storyboard.
// Register cell classes
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
Simply delete that line.
In older Swift versions the line is:
// Register cell classes
self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
I face this problem , because in the viewDidLoad()
I registered my cell with UICollectionViewCell class . In my cellForItemAtIndexPath
used CustomSubcalssCollectionViewCell for dequeueReusableCell.
make sure registerClass and dequeueReusableCell are the same class solve the problem
self.registerClass(SomeUICollectionCellClass.self, forCellWithReuseIdentifier: reuseIdentifier)
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! SomeUICollectionCellClass
I received this error when the custom class (on Xcode inspector) still had its class set to the default UICollectionViewCell rather than my custom class for TestProject.CollectionViewCell.
To fix it, simply change the custom class to your implementation.