【问题标题】:Switch Collectionviews Header/Footer via kind通过 kind 切换 Collectionviews 页眉/页脚
【发布时间】: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


【解决方案1】:

原因是我忘记定义页脚的参考大小。没有它,应用程序不会感知到这种类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-02
    • 2015-08-22
    • 2012-03-03
    • 1970-01-01
    • 2015-01-07
    • 2012-01-26
    • 1970-01-01
    相关资源
    最近更新 更多