[英]How to setup a 1 header with multiple sections using UICollectionViewController
I need some help.我需要一些帮助。 I'm struggling on how to setup a view like this one using UICollectionViewController.我正在努力解决如何使用 UICollectionViewController 设置这样的视图。 The data per sections is different.每个部分的数据不同。 Maybe someone can help me.也许有人可以帮助我。 thank you!谢谢你!
----------- | HEAD | ----------- |Section 1| ----------- | A | B | ----------- | C | D | ----------- | E | F | ----------- | foot | ----------- |Section 2| ----------- | A | B | ----------- | C | D | ----------- | E | F | ----------- | foot | -----------
You can have a section check while returning header view through this delegate method您可以在通过此委托方法返回 header 视图时进行部分检查
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
var reusableview = UICollectionReusableView()
if (kind == UICollectionElementKindSectionFooter) {
if section == 0{
reusableview = customHeaderCell //Header View You want
}
}
else{
reuableView = customFooterHeaderCell // Footer View
}
return reusableview
}
Here's a simple implementation, that only overrides an optional method on the UICollectionViewDataSource protocol:这是一个简单的实现,它只覆盖 UICollectionViewDataSource 协议上的一个可选方法:
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let section = indexPath.section
switch section {
case 0:
let TitleHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: homeHeaderReuseIdentifier, for: indexPath) as! TitleHeader
return TitleHeader
default:
let Section1Header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: sectionSpacingHeaderReuseIdentifier, for: indexPath) as! Section1Header
return Section1Header
}
case UICollectionElementKindSectionFooter:
let FooterView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: homeFooterReuseIdentifier, for: indexPath) as! FooterView
return FooterView
default:
return UICollectionReusableView()
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 100.0)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 100.0)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.