'UICollectionViewCell' does not have a member named 'image' [Swift]

1.3k Views Asked by At

I'm setting up a project with a UICollectionView that displays an image with a label. I created the ViewController and the CellViewController, just like its supposed to.

In the CellViewController the code is the following:

class CellController: UICollectionViewCell {

    @IBOutlet weak var image: UIImageView!
    @IBOutlet weak var label: UILabel!


}

But in the ViewController when I'm setting up the image it gives me the error. Here's the code:

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

    let cell: UICollectionViewCell = collection.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CellController

    cell.image.image = UIImage(named: "star")

    return cell
}

And the weirdest thing is, I have a project downloaded from GitHub that is essentially the same as mine, and it works fine.

Thanks in advance!

1

There are 1 best solutions below

0
On BEST ANSWER

Your problem is the :UICollectionViewCell. Although you cast your cell as CellController this makes the type UICollectionViewCell, which does not have an image property. Removing this or replacing it with CellController will fix your problem.

let cell = collection.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CellController

I would also consider renaming your image view to imageView. There's something about the line cell.image.image that doesn't look right!