gpt4 book ai didi

swift - 具有组合布局的 Collection View 不适用于标题 View

转载 作者:行者123 更新时间:2023-12-04 10:12:02 26 4
gpt4 key购买 nike

正在尝试创建 UICollectionView使用 compositionalLayout .它适用于集合 View 单元格,但是当我尝试添加 header 时,它会因 register nib 错误而崩溃:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: SectionHeaderElementKind with identifier headerView - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'



View Controller 的代码实现:
class ViewController: UIViewController {

@IBOutlet weak var collectionView: UICollectionView!

override func viewDidLoad() {
super.viewDidLoad()

collectionView.register(UINib(nibName: "CollectionReusableView", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headerView")
collectionView.register(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cellIdentifier")
collectionView.collectionViewLayout = customLayout()
}

func customLayout() -> UICollectionViewLayout {

let size = NSCollectionLayoutSize(
widthDimension: NSCollectionLayoutDimension.fractionalWidth(1),
heightDimension: NSCollectionLayoutDimension.estimated(44)
)
let item = NSCollectionLayoutItem(layoutSize: size)
let group = NSCollectionLayoutGroup.horizontal(layoutSize: size, subitem: item, count: 1)

let headerSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .absolute(40)
)
let sectionHeader = NSCollectionLayoutBoundarySupplementaryItem(
layoutSize: headerSize,
elementKind: "SectionHeaderElementKind",
alignment: .top
)

let section = NSCollectionLayoutSection(group: group)
section.boundarySupplementaryItems = [sectionHeader]
let layout = UICollectionViewCompositionalLayout(section: section)
return layout
}
}

extension ViewController: UICollectionViewDataSource {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellIdentifier", for: indexPath) as! CollectionViewCell

return cell
}

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let cell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerView", for: indexPath) as! CollectionReusableView

return cell
}
}

我创建的标题 View :
class CollectionReusableView: UICollectionReusableView {

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

}

如果我在这里遗漏了什么,请帮忙。

最佳答案

在构建布局和将可重用 View 注册到集合 View 时,您应该对元素种类使用相同的值。

改变这个:

collectionView.register(UINib(nibName: "CollectionReusableView", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headerView")


collectionView.register(UINib(nibName: "CollectionReusableView", bundle: nil), forSupplementaryViewOfKind: "SectionHeaderElementKind", withReuseIdentifier: "headerView")

关于swift - 具有组合布局的 Collection View 不适用于标题 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61289330/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com