无法在CollectionViewCell中设置imageView

问题描述 投票:0回答:2

在我的代码中,我试图设置UICollectionViewCell的imageView。 imageView在CollectionViewCell类中称为cellImageView。应用加载时,它将尝试初始化该值,但不起作用

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "selectCell", for: indexPath) as! PhraseSelectionCell
    cell.cellImageView?.image = UIImage(named: "lol")
    return cell
}

UICollectionViewCell的类

class PhraseSelectionCell: UICollectionViewCell {
@IBOutlet var cellImageView: UIImageView!

}
ios swift debugging uicollectionviewcell
2个回答
0
投票

添加图片扩展名,如,

cell.cellImageView?.image = UIImage(named: "lol.png")

0
投票

尝试以这种方式调试,然后您可以找到错误的位置。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "selectCell", for: indexPath) as! PhraseSelectionCell

//debug 1
if UIImage(named: "lol") == nil {
    print("image is nil, double check image name")
}

//debug 2
if cell.cellImageView == nil {
    print("cellImageView outlet is nil")
}

cell.cellImageView?.image = UIImage(named: "lol")
return cell

}

© www.soinside.com 2019 - 2024. All rights reserved.