5

I am making a collection view, and have made my own custom collectionCell. I have specified so in the identity inspector, and as far as i can tell, i have done everything right.

The code is

import UIKit

class SubjectCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var label: UILabel!
}

and in my collectionViewController

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! SubjectCollectionViewCell

    return cell
}

I get the following error message

"Could not cast value of type 'UICollectionViewCell' (0x110460820) to 'project.SubjectCollectionViewCell' (0x10eceeda0). (lldb)"

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Norse
  • 628
  • 8
  • 26
  • 4
    Are you sure you are dequeuing the correct cell? Make sure that reuse identifier is correct, and also that you set your custom cell class in the interface builder. – almas Apr 29 '16 at 15:56

3 Answers3

12

refer to : Could not cast value of type 'UICollectionViewCell'

if you see this in your collectionViewController, delete it. self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

Community
  • 1
  • 1
sashawilldo
  • 121
  • 3
3

What happen is that in viewDidLoad(), Xcode automatically genere a line like this to define the cell:

self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

And your are creating your own method to do this, so, you need to erase the line that was automatically generated by Xcode in the viewDidLoad()

-2

Did you register the reuseIdentifier so that it will create an object of the right class?

gnasher729
  • 51,477
  • 5
  • 75
  • 98