当我在情节提要中向UICollectionView
添加ViewController
时,有一个标记为“ Section Header” 的复选框,我如何以编程方式触发该选项(使用CollectionView的出口)
答案 0 :(得分:0)
首先,您应该像这样在HeaderView中注册要使用的HeaderView
collectionView.register(UINib(nibName: HCollectionReusableView.nibName, bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "HCollectionReusableView")
然后您可以调用您的UICollectionViewDataSource
实现,该函数
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HCollectionReusableView", for: indexPath) as! HCollectionReusableView
reusableview.frame = CGRect(0 , 0, self.view.frame.width, headerHight)
//do other header related calls or settups
return reusableview
default:
fatalError("Unexpected element kind")
}
}