Bright Future Bright Future - 1 month ago 6
iOS Question

Collection view cell doesn't recognize its outlet

I have a collection view controller

ItemCollectionVC
, I want to populate its cells with images, this is how I set it:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
cell.cellImage.image = dataSourceItems[indexPath.row].image
// Error

return cell
}



Hoever, there's an error: Value of type
UICollectionViewCell
have no
member
cellImage



The cell is of custom cell
ItemCell
, I set its custom class in story board, and here is
ItemCell
:

class ItemCell: UICollectionViewCell {

@IBOutlet weak var cellImage: UIImageView!

}


I also connected the outlet to the image view of the cell.

So I don't understand why there's an error, anybody knows?

Answer

Just one mistake that you need to define class for cell as! ItemCell

  let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ItemCell
Comments