gpt4 book ai didi

ios - 无法使 : UICollectionElementKindCell 类型的 View 出队

转载 作者:行者123 更新时间:2023-11-28 14:19:59 25 4
gpt4 key购买 nike

我需要一点帮助。我正在尝试使用 UICollectionViewCell(item) 和 UICollectionReusableView(header) 的自定义布局创建带有标题部分的 UICollectionView。在 header 之前,一切都运行良好,但现在由于某种原因我总是收到此错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason:

'could not dequeue a view of
kind: UICollectionElementKindCell with identifier ProductCellView - must register
a nib or a class for the identifier or connect a prototype cell in a storyboard'

我已经在 CollectionView 中注册了这两个文件。这是我的代码:

override func viewDidLoad() {
super.viewDidLoad()

productsCollectionView.delegate = self
productsCollectionView.dataSource = self

productsCollectionView.register(UINib(nibName: "SectionHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "CollectionReusableView");
productsCollectionView.register(UINib(nibName: "HomeProductViewCell", bundle: nil), forCellWithReuseIdentifier: "ProductCellView")
}

现在我将发布一段代码,用于处理标题部分的 View 并导致崩溃。如果我评论那部分代码, Collection View 当然会显示没有标题的常规项目。在这里:

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
//1
switch kind {
//2
case UICollectionElementKindSectionHeader:
//3
if let sectionHeader = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "CollectionReusableView", for: indexPath) as? SectionHeader{

sectionHeader.categoriesCollectionView.register(UINib(nibName: "CategoryViewCell", bundle: nil), forCellWithReuseIdentifier: "CategoryViewCell")
sectionHeader.homeImagesCollectionView.register(UINib(nibName: "HomeImageViewCell", bundle: nil), forCellWithReuseIdentifier: "HomeImageViewCell")

sectionHeader.homeImagesCollectionView.delegate = self
sectionHeader.homeImagesCollectionView.dataSource = self

sectionHeader.categoriesCollectionView.delegate = self
sectionHeader.categoriesCollectionView.dataSource = self

// Add icon to button
let icon = UIImage(named: "magnifying-glass")!
sectionHeader.btnSearch.setImage(icon, for: .normal)
sectionHeader.btnSearch.imageEdgeInsets = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 20)
sectionHeader.btnSearch.titleEdgeInsets = UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 0)
sectionHeader.btnSearch.imageView?.contentMode = .scaleAspectFit
sectionHeader.btnSearch.layer.cornerRadius = 4

sectionHeader.prepareCategories()
sectionHeader.prepareHomeImages()

return sectionHeader
}
default:
//4
assert(false, "Unexpected element kind")
}
return UICollectionReusableView()
}

所以我真的很绝望,因为我花了将近两天的时间来寻找为什么会发生这种情况的错误。如果有人指出正确的方向寻找错误,我将不胜感激,因为我已经尝试了几乎所有我知道的方法。

最佳答案

问题出在内部集合中,因为您在此处设置了委托(delegate)和数据源

sectionHeader.homeImagesCollectionView.delegate = self
sectionHeader.homeImagesCollectionView.dataSource = self
sectionHeader.categoriesCollectionView.delegate = self
sectionHeader.categoriesCollectionView.dataSource = self

它要求

 if let sectionHeader = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "CollectionReusableView", for: indexPath) as? SectionHeader{

所以你必须将整个部分包裹在集合类型中

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {


if collectionView == mainCollection {

}
else
if collectionView == homeImagesCollectionView {

}
else { // categoriesCollectionView

}

}

BTW 寄存器应该在 viewDidLoad 中用于 VC 中的集合,以及在 init/awakeFromnib 中的 collectionCell 自定义类中

关于ios - 无法使 : UICollectionElementKindCell 类型的 View 出队,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51975895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com