无法使同类视图出队:UICollectionElementKindCell

时间:2018-08-22 22:15:36

标签: ios swift uicollectionview

我需要一点帮助。我正在尝试使用UICollectionViewCell(item)和UICollectionReusableView(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")
}

现在我将发布一个代码块,该代码块用于处理标头部分的视图并导致崩溃。如果我注释那部分代码,则集合视图将显示没有标题的常规项目。在这里:

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()
}

所以我在这里真的很绝望,因为我已经损失了将近两天的时间来寻找错误的原因。如果有人指出正确的方向寻找错误,我将不胜感激,因为我已经尝试了几乎所有我知道的东西。

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

在注册笔尖之前获取数据并重新加载表格视图时,我遇到了类似的问题。鉴于您提供的信息,这是我最好的答案。