CollectionView 部分 Header 未显示
CollectionView section Header not displaying
当我最初向我的 collectionView 添加一个 sectionHeader 时,它确实显示正常。我的 collectionViewController.swift 里有这个, header.
里有一个 class
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()
后,header 不再出现。
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()
我用它来强制它在所有屏幕尺寸上显示两行单元格。 (尽管我仍然无法调整单元格中所有内容的大小)。
我不确定为什么 header 不再出现
(我正在使用 Swift 3)
您需要实施 header 高度方法。
添加layout.headerReferenceSize = CGSize(100, 200)//size you want header in every section
如果你只想要一个header在特殊部分,你可以实现
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,referenceSizeForHeaderInSection section: Int) -> CGSize
这个方法代替
更何况,我看你没有注册header查看。
我想你也可以调用这个方法register(_ viewClass: AnyClass?, forSupplementaryViewOfKind elementKind: String, withReuseIdentifier identifier: String)
来注册header
当我最初向我的 collectionView 添加一个 sectionHeader 时,它确实显示正常。我的 collectionViewController.swift 里有这个, header.
里有一个 classoverride 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()
后,header 不再出现。
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()
我用它来强制它在所有屏幕尺寸上显示两行单元格。 (尽管我仍然无法调整单元格中所有内容的大小)。
我不确定为什么 header 不再出现
(我正在使用 Swift 3)
您需要实施 header 高度方法。
添加layout.headerReferenceSize = CGSize(100, 200)//size you want header in every section
如果你只想要一个header在特殊部分,你可以实现
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,referenceSizeForHeaderInSection section: Int) -> CGSize
这个方法代替
更何况,我看你没有注册header查看。
我想你也可以调用这个方法register(_ viewClass: AnyClass?, forSupplementaryViewOfKind elementKind: String, withReuseIdentifier identifier: String)
来注册header