Ios CollectionView节标题未显示
当我最初在collectionView中添加一个sectionHeader时,它看起来确实很正常。我在我的collectionViewController.swift中有这个,并且有一个标题类Ios CollectionView节标题未显示,ios,swift,uicollectionview,uicollectionviewlayout,Ios,Swift,Uicollectionview,Uicollectionviewlayout,当我最初在collectionView中添加一个sectionHeader时,它看起来确实很正常。我在我的collectionViewController.swift中有这个,并且有一个标题类 override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollect
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)您需要实施收割台高度方法 添加
layout.headerReferenceSize=CGSize(100200)//在每个节中添加标题的大小
如果您只想在特殊部分中使用一个标题,则可以实现
public func collectionView(\ucollectionview:UICollectionView,layout collectionViewLayout:UICollectionViewLayout,referenceSizeForHeaderInstruction节:Int)->CGSize
而不是使用这种方法
更重要的是,我看到你没有注册headerView。
我想您也可以调用此方法register(\uviewclass:AnyClass?,对于种类元素种类:String的SupplementViewofKind,withReuseIdentifier标识符:String)
来注册标头