好久沒有用collectionView了,一下子居然忘記很多病梢,所以還是記下來瘦赫,以后方便找辰晕。。确虱。
添加組頭:
- 注意注冊: 注冊一定要用系統(tǒng)的UICollectionElementKindSectionHeader字符串
self.register(KRKnapsackHeaderView.classForCoder(),forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HEADERVIEW")
- 注意 獲取組頭的方法
一定要從緩存池里面獲取
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let headerView = dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: HEADERVIEW, for: indexPath)
headerView.backgroundColor = UIColor.c_0xf8f8f8
return headerView
}
- 設(shè)置組頭大小
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize{
return CGSize(width: screenWidth, height: kViewCurrentH_XP(H: 20))
}