Could not cast value of type 'UICollectionViewCell'
Solution 1
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)
Solution 2
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
Solution 3
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.
Solution 4
I hit this error when instantiating a ViewController
from a Storyboard in my unit test.
storyboard.instantiateViewControllerWithIdentifier("someVC") as! MyViewController
The problem was that I had assigned a specific module in the Identity inspector to that VC in the Storyboard. I removed it so it defaults to the current module which is the application module while running and the Test module while testing.
Solution 5
self.collectionView!
.register(MyCollectionViewCell.self,
forCellWithReuseIdentifier: reuseIdentifier
)
You don't have to delete this line. You will register your own cell class inherited by UICollectionViewCell.self
if you want to register class using code
Related videos on Youtube
coco
iOS Developer (since 2009), React Native (2017-2019), UX/UI Designer (Sketch/Zeplin), SwiftUI (since 2020)
Updated on July 08, 2022Comments
-
coco almost 2 years
I use a custom CollectionViewCell in my Storyboard. When I start the app I get this message:
Could not cast value of type 'UICollectionViewCell' to TestProject.CollectionViewCell.
-
Dave G over 8 yearsThis was causing an error for me and, upon deleting, the view does now load. However, now the UICollectionView looks terrible. I believe deleting that line of code means all my layout constraints that I'd set in my Storyboard, are now junk and have to be added programmatically?
-
coco over 8 yearsI think the constraint problem has another cause. From Apple docs: "Prior to calling the dequeueReusableCellWithReuseIdentifier:forIndexPath: method of the collection view, you must use this method or the registerNib:forCellWithReuseIdentifier: method to tell the collection view how to create a new cell of the given type." My experience is, that if the cell is defined in a storyboard the registerNib seems to work automatically.
-
Wizkid over 8 yearsGeneral note: It is not necessary to register cell classes when using Storyboard prototypes. If you register, it will supersede your prototype.
-
coco about 5 years@nyxee thanks for the note, that the code changed in the meantime. I updated the answer.
-
touhid udoy about 4 yearsafter 2 hours of trying, i did a google search and found this excellent answer. thanks a lot.
-
lukas28277 over 3 yearsThis literally saved me, I tried so many different ways, only you had the right answear