ios - 无法在 CollectionViewCell 中设置 imageView
问题描述
在我的代码中,我试图设置 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
}
推荐阅读
- javascript - 填充
- javascript - Flask Socket IO,需要帮助来理解为什么消息会变大
- php - 从 WooCommerce 优惠券中排除具有特定属性条款的产品
- vue.js - [Vue 警告]:无法挂载组件:未定义模板或渲染函数,使用 Laravel 8
- java - 如何为登录添加额外的弹簧安全字段
- c# - 仅在一个方向上更改对象的比例
- node.js - .push 不是网络爬虫中的功能
- python-3.x - Buildozer 无法编译 .apk,因为未定义“xrange”
- dispose - 为什么我从来没有得到 ObjectDisposedException?
- operators - 什么是部分和不安全的运算符?