无法Dequeue viewForSupplementaryElementOfKind'返回的视图没有被检索到'。

问题描述 投票:0回答:1

我有一个 collectionView 有2个部分。我正试图 dequeueReusableSupplementaryView 然而。 我得到以下错误。

*** 由于未捕获异常'NSInternalInconsistencyException'而终止应用程序,原因是:'调用-dequeueReusableSupplementaryViewOfKind:viewForSupplementaryElementOfKind:atIndexPath (UICollectionElementKindSectionFooter, {length = 2, path = 1 - 0})返回的视图没有被调用-dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath检索到:或者是nil

这是我的代码。

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
}()
ios swift uicollectionview uicollectionreusableview
1个回答
1
投票

该错误信息告诉你,你返回的视图要么是(a)没有由 dequeueReusableSupplementaryViewOfKind;或(b)是 nil. 从错误信息来看,它似乎是在与 UICollectionElementKindSectionFooter 为第二节。

你正在返回 UICollectionReusableView() 在不少地方。这是不正确的,会导致这个错误。您必须返回一个实际的已排队的补充视图(或配置您的集合视图,以便使 viewForSupplementaryElementOfKind 不会被调用的那一节种)。)

© www.soinside.com 2019 - 2024. All rights reserved.