ios - 无法出列同类视图:UICollectionElementKindCell
问题描述
我需要一点帮助。UICollectionViewCell
我正在尝试使用(项目)和UICollectionReusableView
(标题)的自定义布局创建带有标题部分的 UICollectionView 。在标头之前,一切都运行良好,但现在由于某种原因我总是收到此错误:
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()
}
所以我在这里真的很绝望,因为我已经失去了将近两天的时间来寻找为什么会发生这种情况的错误。如果有人指出我在哪里寻找错误的正确方向,我将不胜感激,因为我已经尝试了几乎所有我知道的东西。
解决方案
问题出在内部集合中,因为您在此处设置了委托和数据源
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
}
}
顺便说一句,寄存器应该在viewDidLoad
VC 内部的集合中,并且在collectionCell 自定义类中init
/内部awakeFromnib
推荐阅读
- java - 使用 AlarmManager 和 BroadcastReciver 创建警报
- c# - 如何检查 2 个触发器是否会发生冲突?
- powershell - 流水线 Powershell cmdlet 显示部分结果?
- python - 遍历字典 即时更新值 Python3
- r - R不想安装包,我不能改变工作目录
- c - 如果指针可以转换为任何类型(在 c 中),为什么要使用 void 指针?
- python - 如果有可用空间,如何缩小外部 QWidget/顶级窗口?
- iis - 使用 Regex 重写规则在 IIS 上不起作用
- javascript - 如何在内部从事件侦听器调用类函数?
- android - CreateFragment 在通过按钮调用 viewPager.setCurrentItem 时调用了两次