在我的代码中,我试图设置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!
}
添加图片扩展名,如,
cell.cellImageView?.image = UIImage(named: "lol.png")
尝试以这种方式调试,然后您可以找到错误的位置。
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
}