简体   繁体   English

如何使用 UICollectionViewController 设置具有多个部分的 1 header

[英]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.

相关问题 使UICollectionViewController页面部分 - Make UICollectionViewController paged sections 如何在 UICollectionViewController 的标头中访问 UISegmentedControl? - How can I access UISegmentedControl in header of UICollectionViewController? 在故事板中具有多个部分的UICollectionView设置 - UICollectionView setup with multiple sections in storyboard 标头没有显示在UICollectionViewController Swift中 - Header not showing in UICollectionViewController Swift 在UICollectionViewController Header上添加UISearchBar - Add UISearchBar on UICollectionViewController Header UICollectionViewController页脚/页眉未显示 - UICollectionViewController Footer/ Header not showing Swift - 如何使用枚举为 UITableView 制作自定义标题部分? - Swift - how to make custom header sections for UITableView using enum? 如何使用Swift 3在MutableArray的标头中在TableView中创建字母部分 - How to create alphabetic sections in TableView with header from MutableArray using swift 3 Swift:UICollectionViewController的标头与单元格重叠 - Swift: Header of UICollectionViewController overlaps cells UICollectionView顶部具有多个部分的单个标题视图 - Single Header View at top of UICollectionView with multiple sections
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM