迅速:更改UICollectionView

时间:2018-11-22 13:58:55

标签: swift uicollectionview

当我在情节提要中向UICollectionView添加ViewController时,有一个标记为“ Section Header” 的复选框,我如何以编程方式触发该选项(使用CollectionView的出口)

1 个答案:

答案 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")
    }
}