Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can i not downcast UICollectionViewCell to a subclass thereof?

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)"

like image 475
Norse Avatar asked Apr 29 '16 15:04

Norse


2 Answers

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)

like image 153
sashawilldo Avatar answered Nov 15 '22 12:11

sashawilldo


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()

like image 44
juanmjimenezs Avatar answered Nov 15 '22 12:11

juanmjimenezs