我正在编写一个程序来拍照并在集合视图中显示它们。
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return myImages.count
}
以下功能为cellForItemAt
,这是cell.imageView.image = image
行发生错误的地方。
给出的错误是:
'UICollectionViewCell'类型的值没有成员'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
}
我的关于collectionCell的类声明如下。它通过故事板等连接到collectionView,所以我不确定错误发生的原因。
class collectionCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
}
答案 0 :(得分:2)
编写类名而不是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
}
答案 1 :(得分:1)
您需要cast
UICollectionViewCell
您自己的单元格类型(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
}
错误的部分:
... as UICollectionViewCell
应该是:
... as! collectionCell