【问题标题】:CollectionView section Header not displayingCollectionView 部分标题未显示
【发布时间】:2017-06-30 20:49:31
【问题描述】:

当我最初将 sectionHeader 添加到我的 collectionView 时,它确实看起来很正常。我在我的 collectionViewController.swift 中有这个,并且有一个标题类。

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

        return header
    }

一旦我将它添加到viewDidLoad(),标题就不再出现了。

 let cellsAcross: CGFloat = 2
        let spaceBetweenCells: CGFloat = 5
        let dimX = ((collectionView?.bounds.width)! - (cellsAcross - 1) * spaceBetweenCells) / cellsAcross
        let dimY = CGFloat(dimX/187 * 125)
        let cellSize = CGSize(width: dimX , height: dimY)

        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.itemSize = cellSize
        //layout.sectionInset = UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1)
        layout.minimumLineSpacing = 5.0
        layout.minimumInteritemSpacing = 5.0
        collectionView?.setCollectionViewLayout(layout, animated: true)
        collectionView?.reloadData()

我有这个强制它在所有屏幕尺寸上显示两行单元格。 (我仍然有一个问题,即没有调整单元格中所有内容的大小)。

我不确定为什么标题不再出现

(我使用的是 Swift 3)

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewlayout


    【解决方案1】:

    你需要实现头部高度方法。

    添加layout.headerReferenceSize = CGSize(100, 200)//size you want header in every section

    如果你只想在特殊部分有一个标题,你可以实现

    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,referenceSizeForHeaderInSection section: Int) -> CGSize 改用这个方法

    还有,我看你没有注册headerView。 我想你也可以调用这个方法register(_ viewClass: AnyClass?, forSupplementaryViewOfKind elementKind: String, withReuseIdentifier identifier: String)来注册header

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-19
      • 2014-10-26
      • 1970-01-01
      相关资源
      最近更新 更多