在我的代码中,我试图设置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!
}
最佳答案
尝试以这种方式进行调试,然后您可以找到错误所在。
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
}
关于ios - 无法在CollectionViewCell中设置imageView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61072666/