我需要帮助。我正在努力研究如何使用UICollectionViewController设置这样的视图。每个部分的数据不同。也许有人能帮我。谢谢您!

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

最佳答案

下面是一个简单的实现,它只重写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)
}

10-07 15:24