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

时间:2019-11-13 04:56:45

标签: ios swift iphone user-interface uicollectionview

我需要一些帮助。我正在努力使用UICollectionViewController设置像这样的视图。每节的数据不同。也许有人可以帮助我。谢谢!

    -----------   
    |   HEAD  |
    -----------
    |Section 1|
    -----------
    | A  |  B |
    -----------
    | C  |  D |
    -----------
    | E  |  F |
    -----------
    |   foot  |
    -----------
    |Section 2|
    -----------
    | A  |  B |
    -----------
    | C  |  D |
    -----------
    | E  |  F |
    -----------
    |   foot  |
    -----------

2 个答案:

答案 0 :(得分:2)

通过此委托方法返回标题视图时,您可以进行节检查

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
}

答案 1 :(得分:1)

这是一个简单的实现,仅覆盖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)
}