【问题标题】:Collection View how to Create TableView HeaderCollection View如何创建TableView Header
【发布时间】:2021-01-11 15:20:09
【问题描述】:

我想为我的Collection 视图创建一个headerView。 但它不能是节标题。我只是想在我的Collection 视图中看到一个headerView

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

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

【问题讨论】:

标签: ios swift mobile uicollectionview header


【解决方案1】:

您可以在您的收藏视图中添加补充视图并根据需要自定义它,请this教程帮助您

【讨论】:

  • 阿拉米感谢您的评论。但不幸的是,它只显示部分的标题。但我需要没有它(部分)
  • 你能提供一张你想做的图片或草图吗?
  • 我有一个收藏视图。其中包含许多部分联赛名称和事件。例如 section 0 : Premier League-> 行内的 Section name Manchester vs Chelsea 。第 1 节:西班牙联赛 - 皇马 vs 巴萨。我想将 collectionview 的横幅视图顶部创建为可滚动的。这就是为什么我必须实现collectionview header view。但不是节标题。
  • 也许你可以通过在你的collectionview上方设置一个uiview并通过实现你的collection view的didscroll来控制它的高度来实现你想要的。这可能会有所帮助stackoverflow.com/a/49931458/11543623
  • 感谢您的帮助。它实际上并没有帮助我,但你的支持让我留在了盒子里:D。我解决了添加另一个标题部分的问题。但我只将它用于第 0 节。一些集合视图协议方法为此发生了变化。希望我的解决方案看起来不错。
【解决方案2】:

想法很简单,添加一个section,用于headerSection,由其他section reader分隔。

 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
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-10
    • 1970-01-01
    相关资源
    最近更新 更多