我需要一点帮助。我正在尝试使用UICollectionViewCell(item)和UICollectionReusableView(header)的自定义布局创建带有header部分的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")
}

现在我将发布一个代码块,用于处理头部分的视图,这将导致崩溃。如果我注释了代码的那一部分,collection视图将显示没有标题的常规项。这里是:
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

   }

}

BTW寄存器应该在VC内部的集合的viewDidLoad中,在collectionCell自定义类的init/awakeFromnib

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

10-12 01:49