【发布时间】:2019-02-09 15:28:03
【问题描述】:
我的 CollectionView 中有一个页眉和一个页脚,我希望它们都显示出来。在当前状态下,我得到了 2 倍的 Header,因为 Section 的“种类”不会自动改变,它两次进入 case “UICollectionElementKindSectionHeader”。
我可以切换 indexPath 而不是 kind,但我也想知道另一种方法。
func setupCollectionView() {
collectionView?.backgroundColor = .white
collectionView?.register(ProfileHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: ProfileHeader.reuseIdentifier)
collectionView?.register(ProfileFooter.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: ProfileFooter.reuseIdentifier)
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: ProfileHeader.reuseIdentifier, for: indexPath) as! ProfileHeader
header.user = self.user
return header
case UICollectionElementKindSectionFooter:
let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: ProfileFooter.reuseIdentifier, for: indexPath) as! ProfileFooter
return footer
default:
assert(false, "Unexpected element kind")
}
}
【问题讨论】:
-
你目前的方法非常好......
-
我得到了两次headerview ..(正如我在问题中所写)
标签: ios swift header switch-statement collectionview