Could not cast value of type 'UICollectionViewCell'


Xcode Problem Overview

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.

Xcode Solutions

Solution 1 - Xcode

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 - Xcode

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 - Xcode

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.

Custom Class Section

Document Outline

Solution 4 - Xcode

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 - Xcode

               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

Solution 6 - Xcode

You will get this error if you register your cell class both in Storyboard and in code. Choose one or the other, never both and the problem goes away.

I wish Apple didn't include this bit of code in their templates:

self.registerClass(SomeUICollectionCellClass.self, forCellWithReuseIdentifier: reuseIdentifier)

Especially since they always recommend you register your cells through Storyboard. Makes things very confusing.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncocoView Question on Stackoverflow
Solution 1 - XcodecocoView Answer on Stackoverflow
Solution 2 - XcodeStephen ChenView Answer on Stackoverflow
Solution 3 - XcodeKKGView Answer on Stackoverflow
Solution 4 - XcodeSebastianView Answer on Stackoverflow
Solution 5 - XcodeUmair KhalidView Answer on Stackoverflow
Solution 6 - XcodekakubeiView Answer on Stackoverflow