如何解决UICollectionView 单元格 contentView 完全空白
我面临一个问题。而且我认为这可能是一个奇怪的问题。我正在使用 UICollectionViewController 并像这样配置它
class ViewController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
guard let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else { return }
flowLayout.minimumLineSpacing = 10
flowLayout.itemSize = CGSize(width: UIScreen.main.bounds.width - 20,height: 60)
flowLayout.sectionInset = UIEdgeInsets(
top: 10,left: 10,bottom: 10,right: 10
)
collectionView.register(DemoTakesCollectionViewCell.self,forCellWithReuseIdentifier: DemoTakesCollectionViewCell.identifier)
}
override func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
return 2
}
override func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: DemoTakesCollectionViewCell.identifier,for: indexPath) as? DemoTakesCollectionViewCell else {
fatalError("UICollectionView must be downcasted to CollectionViewCell")
}
return cell
}
}
我也为 UICollectionView
注册了一个单元格,但它的内容没有以某种方式显示。在视图层次结构中,单元格在那里,但不是它的内容。这里可能有什么问题?
解决方法
啊……您在问题中遗漏了一条重要信息:
“这是xib”
所以这一行:
collectionView.register(DemoTakesCollectionViewCell.self,forCellWithReuseIdentifier: DemoTakesCollectionViewCell.identifier)
注册类 -- 但该类没有添加任何 UI 元素。
您需要注册笔尖:
let nib = UINib(nibName: "DemoTakesCollectionViewCell",bundle: nil)
collectionView.register(nib,forCellWithReuseIdentifier: DemoTakesCollectionViewCell.identifier)
这应该可以解决问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。