集合视图如何创建TableView标头



我想为我的Collection视图创建一个headerView。但它不需要是一个节头。我只想在Collection视图的顶部看到一个headerView

我的章节已经有了一个章节标题。我不需要。我研究了很多,但没有找到任何解决方案。

我希望有人能帮我。这是headerView需要按预期滚动查看我的collectionview

您可以在集合视图中添加一个补充视图,并根据自己的喜好进行自定义,本教程可以帮助您吗

想法很简单,添加一个部分并将其用于由其他部分读取器分隔的头部分。

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
return 0
}
func numberOfSections(in collectionView: UICollectionView) -> Int {

return viewModel.getNumberOfSection() + 1
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {

if section == 0  {
return .init(width: view.frame.width, height: 100)
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {


if kind == UICollectionView.elementKindSectionHeader && indexPath.section == 0 {
let headerView: headerView  = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier:"headerView", for: indexPath) as! headerView
return headerView
}