首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法排排类型的视图: UICollectionElementKindCell

无法排排类型的视图: UICollectionElementKindCell
EN

Stack Overflow用户
提问于 2018-08-22 22:15:35
回答 2查看 3.9K关注 0票数 1

我需要一点帮助。我正在尝试使用UICollectionViewCell(item)和UICollectionReusableView(标头)的自定义布局来创建带有标头部分的UICollectionReusableView。在标题之前,一切都运行得很好,但是现在出于某种原因,我总是会遇到这样的错误:

代码语言:javascript
复制
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中注册了这两个文件。这是我的代码:

代码语言:javascript
复制
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")
}

现在,我将发布一段代码,用于处理标题部分的视图,并导致崩溃。如果我注释这部分代码,集合视图当然会显示没有标题的常规项。下面是:

代码语言:javascript
复制
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的正确方向,我会非常感激的,因为我已经尝试了我所知道的几乎所有的东西。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-22 22:28:21

问题在内部集合中,因为您在这里设置了委托& dataSource

代码语言:javascript
复制
sectionHeader.homeImagesCollectionView.delegate = self
sectionHeader.homeImagesCollectionView.dataSource = self
sectionHeader.categoriesCollectionView.delegate = self
sectionHeader.categoriesCollectionView.dataSource = self

它要求

代码语言:javascript
复制
 if let sectionHeader = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "CollectionReusableView", for: indexPath) as? SectionHeader{

因此,您必须将整个部分包装在集合的类型内。

代码语言:javascript
复制
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中应该有寄存器。

票数 2
EN

Stack Overflow用户

发布于 2018-08-22 22:26:54

在注册nib之前,我在获取数据和重新加载表视图时遇到了类似的问题。鉴于你提供的信息,这是我最好的回答。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51975895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档