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.

coco
  • 4,912
  • 2
  • 25
  • 33

6 Answers6

211

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)
coco
  • 4,912
  • 2
  • 25
  • 33
  • 1
    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? – Dave G Aug 25 '15 at 15:51
  • 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. – coco Aug 26 '15 at 13:41
  • 9
    General note: It is not necessary to register cell classes when using Storyboard prototypes. If you register, it will supersede your prototype. – Wizkid Oct 29 '15 at 16:31
  • @nyxee thanks for the note, that the code changed in the meantime. I updated the answer. – coco Feb 20 '19 at 09:14
  • after 2 hours of trying, i did a google search and found this excellent answer. thanks a lot. – touhid udoy Apr 02 '20 at 10:26
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
Stephen Chen
  • 3,027
  • 2
  • 27
  • 39
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

KKG
  • 131
  • 1
  • 3
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.

Sebastian
  • 2,109
  • 1
  • 20
  • 15
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

s-hunter
  • 24,172
  • 16
  • 88
  • 130
Umair Khalid
  • 2,259
  • 1
  • 21
  • 28
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.

kakubei
  • 5,321
  • 4
  • 44
  • 66