【发布时间】: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