UICollectionView cell contentView 完全空白
UICollectionView cell contentView completely blank
我遇到了一个问题。我认为这可能很奇怪。我正在使用 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
注册了一个单元格,但它的内容不知何故没有显示出来。在 View Hierarchy 中,Cell 在那里,但不是它的内容。这可能是什么问题?
啊...您在问题中遗漏了一条重要信息:
“是xib”
所以这一行:
collectionView.register(DemoTakesCollectionViewCell.self, forCellWithReuseIdentifier: DemoTakesCollectionViewCell.identifier)
注册 class -- 但 class 没有添加任何 UI 元素。
您需要注册 nib:
let nib = UINib(nibName: "DemoTakesCollectionViewCell", bundle: nil)
collectionView.register(nib, forCellWithReuseIdentifier: DemoTakesCollectionViewCell.identifier)
这应该可以解决问题。
我遇到了一个问题。我认为这可能很奇怪。我正在使用 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
注册了一个单元格,但它的内容不知何故没有显示出来。在 View Hierarchy 中,Cell 在那里,但不是它的内容。这可能是什么问题?
啊...您在问题中遗漏了一条重要信息:
“是xib”
所以这一行:
collectionView.register(DemoTakesCollectionViewCell.self, forCellWithReuseIdentifier: DemoTakesCollectionViewCell.identifier)
注册 class -- 但 class 没有添加任何 UI 元素。
您需要注册 nib:
let nib = UINib(nibName: "DemoTakesCollectionViewCell", bundle: nil)
collectionView.register(nib, forCellWithReuseIdentifier: DemoTakesCollectionViewCell.identifier)
这应该可以解决问题。