我需要一点帮助。我正在尝试使用UICollectionViewCell(item)和UICollectionReusableView(标头)的自定义布局来创建带有标头部分的UICollectionReusableView。在标题之前,一切都运行得很好,但是现在出于某种原因,我总是会遇到这样的错误:
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")
}现在,我将发布一段代码,用于处理标题部分的视图,并导致崩溃。如果我注释这部分代码,集合视图当然会显示没有标题的常规项。下面是:
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()
}所以我在这里非常绝望,因为我已经浪费了将近两天的时间,找到了为什么会发生这种情况的错误。如果有人能为我指明寻找bug的正确方向,我会非常感激的,因为我已经尝试了我所知道的几乎所有的东西。
发布于 2018-08-22 22:28:21
问题在内部集合中,因为您在这里设置了委托& dataSource
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
}
}顺便说一句,VC内部的集合应该在viewDidLoad内部,collectionCell自定义类中的init/awakeFromnib中应该有寄存器。
发布于 2018-08-22 22:26:54
在注册nib之前,我在获取数据和重新加载表视图时遇到了类似的问题。鉴于你提供的信息,这是我最好的回答。
https://stackoverflow.com/questions/51975895
复制相似问题