【问题标题】:Unable to Dequeue viewForSupplementaryElementOfKind 'the view returned was not retrieved'Unable to Dequeue viewForSupplementaryElementOfKind '未检索到返回的视图'
【发布时间】:2020-05-10 16:05:54
【问题描述】:

我有一个 collectionView 有 2 个部分。但是,我正在尝试dequeueReusableSupplementaryView。我收到以下错误:

*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“视图返回自 -collectionView:viewForSupplementaryElementOfKind:atIndexPath (UICollectionElementKindSectionFooter, {length = 2, path = 1 - 0}) 未被检索到 打电话 -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: 或为零

这是我的代码:

enum HomeSection: Int, CaseIterable {
    case companies = 0, reviews
}

private var sections = [HomeSection]()

override func viewDidLoad() {
        super.viewDidLoad()
        sections = [.companies, .reviews]
        setUpView()
    }

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let companyCell = collectionView.dequeueReusableCell(withReuseIdentifier: Identifier.companyCell, for: indexPath) as! CompanyCell
        let reviewCell = collectionView.dequeueReusableCell(withReuseIdentifier: Identifier.reviewCell, for: indexPath) as! ReviewCell
        switch sections[indexPath.section] {
        case .companies:
            let company = self.companies[indexPath.item]
            companyCell.company = company
            return companyCell
        case .reviews:
            let review = self.reviews[indexPath.item]
            reviewCell.review = review
            return reviewCell
        }
    }

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    switch sections[indexPath.section] {
    case .companies:
        switch kind {
        case UICollectionView.elementKindSectionHeader:
            let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: Identifier.headerView, for: indexPath) as! HeaderView
            return headerView
        case UICollectionView.elementKindSectionFooter:
            let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: Identifier.footerView, for: indexPath) as! FooterView
            return footerView
        default:
            return UICollectionReusableView()
        }
    case .reviews:
        switch kind {
        case UICollectionView.elementKindSectionHeader:
            return UICollectionReusableView()
        case UICollectionView.elementKindSectionFooter:
            return UICollectionReusableView()
        default:
            return UICollectionReusableView()
        }
    }
}

我已确保我已注册所有单元格和补充视图。

注册的单元格、页眉和页脚

private let collectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.sectionHeadersPinToVisibleBounds = true
    let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
    view.translatesAutoresizingMaskIntoConstraints = false
    view.backgroundColor = .white
    view.alwaysBounceVertical = true
    view.register(CompanyCell.self,
                  forCellWithReuseIdentifier: Identifier.companyCell)
    view.register(ReviewCell.self,
                  forCellWithReuseIdentifier: Identifier.reviewCell)
    view.register(HeaderView.self,
                  forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,
                  withReuseIdentifier: Identifier.headerView)
    view.register(FooterView.self,
                  forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter,
                  withReuseIdentifier: Identifier.footerView)
    return view
}()

【问题讨论】:

  • 您能显示您注册为 supp 的单元格以及如何注册它们
  • 我已经用注册的单元格、页眉和页脚更新了问题
  • 你是通过编程还是 xib 创建它们?
  • 全部以编程方式。他们目前只持有一个 UILabel,当然还有所需的初始化程序。问题似乎出在 IndexPath 上,但是我相信我已经正确切换了这些部分?

标签: ios swift uicollectionview uicollectionreusableview


【解决方案1】:

错误消息告诉您,您正在返回的视图 (a) 不是由 dequeueReusableSupplementaryViewOfKind 返回的;或 (b) 是nil。从错误信息来看,第二部分的UICollectionElementKindSectionFooter 似乎有问题。

您在很多地方都返回UICollectionReusableView()。这是无效的,将导致此错误。您必须返回一个实际出队的补充视图(或配置您的集合视图,以便不会为该部分类型调用 viewForSupplementaryElementOfKind)。

【讨论】:

  • 如果视图应该返回 nil,则通过添加 assert(false, "Unexpected element kind") 解决了这个问题。
  • 据我所知,asserts 仅适用于调试,不适用于发布版本。
  • 是的。因此,您仍然必须返回虚拟值,否则编译器会抱怨。我个人使用fatalError,编译器知道执行无法继续,并将在此基础上验证您的代码(例如,不仅让您免于返回一些虚拟值,甚至警告您不要输入无法访问的代码。@987654328 @ 是另一种选择。
【解决方案2】:

如果你不使用 UICollectionViewController 必须调用 init()

内的寄存器

或者如果你在 UIViewController 中创建一个 collectionView,你必须将 collectionView 声明为 lazy var 而不是 let

示例

private let headerIdentifer = "Cell"
private let reuseIdentifer = "Header"

private lazy var collectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.delegate = self
    cv.dataSource = self
    cv.register(CellView.self, forCellWithReuseIdentifier: reuseIdentifer)
    cv.register(HeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: headerIdentifer)
    cv.backgroundColor = .clear
    cv.showsVerticalScrollIndicator = false
    return cv
}()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    • 2017-12-06
    • 2022-12-27
    • 2021-03-27
    • 2023-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多