57

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.

6 Answers 6

209

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) 
Sign up to request clarification or add additional context in comments.

5 Comments

This 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?
I 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.
General note: It is not necessary to register cell classes when using Storyboard prototypes. If you register, it will supersede your prototype.
@nyxee thanks for the note, that the code changed in the meantime. I updated the answer.
after 2 hours of trying, i did a google search and found this excellent answer. thanks a lot.
48

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 

Comments

11

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

Comments

5

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.

Comments

4
 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

Comments

2

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.

1 Comment

This literally saved me, I tried so many different ways, only you had the right answear

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.