Reputation: 478
I'm writing a program that takes pictures and displays them in a collection view.
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return myImages.count
}
Function below is cellForItemAt
this is where the error occurs on the cell.imageView.image = image
line.
The error given is:
Value of type 'UICollectionViewCell' has no member 'imageView'
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as UICollectionViewCell
let image = myImages[indexPath.row]
cell.imageView.image = image
return cell
}
My class declaration for the collectionCell is below. It's connected to the collectionView via the storyboard etc so I'm unsure why the error occurs.
class collectionCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
}
Upvotes: 0
Views: 2979
Reputation: 2430
write class name instead of UICollectionCell
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! collectionCell
let image = myImages[indexPath.row]
cell.imageView.image = image
return cell
}
Upvotes: 3
Reputation: 11435
You need to cast
your UICollectionViewCell
to your own type of cell (collectionCell
):
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! collectionCell
let image = myImages[indexPath.row]
cell.imageView.image = image
return cell
}
The wrong part:
... as UICollectionViewCell
should be:
... as! collectionCell
Upvotes: 1