【问题标题】:viewForSupplementaryElementOfKind is not getting calledviewForSupplementaryElementOfKind 没有被调用
【发布时间】:2019-07-25 18:58:50
【问题描述】:

我已经声明了如下的集合视图

lazy var collectionView:UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize  = UICollectionViewFlowLayout.automaticSize
        layout.minimumLineSpacing = 52
        layout.sectionInset = UIEdgeInsets(top: 25, left: 0, bottom: 30, right: 0)
        var collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.white
        collectionView.showsVerticalScrollIndicator = false
        collectionView.register(XXXCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        collectionView.register(XXXHeaderViewCell.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: headerIdentifier)
        collectionView.delegate = self
        return collectionView
    }()

并在委托下实现,控制权仅来自 referenceSizeForHeaderInSection 方法,而不是 viewForSupplementaryElementOfKind

public func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerIdentifier, for: indexPath)
        return headerView
    }

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        let size = CGSize(width: self.collectionView.bounds.width, height: 200)
        return size
    }

非常感谢任何帮助。

【问题讨论】:

  • 修复了这个问题。我有一个 customDataSource,它有这个方法的实现。

标签: ios uicollectionview


【解决方案1】:

我遇到了同样的问题,然后想出了一个解决方案。

尝试在您的func collectionView(viewForSupplementaryElementOfKind:at:) 上方添加@objc (collectionView:viewForSupplementaryElementOfKind:atIndexPath:)

obj-C 和 Swift 似乎有不同的名称。 看看最后一个参数,在 obj-C 上我们有 atIndexPath 而在 Swift 上我们只有 at

这有帮助吗?

【讨论】:

    猜你喜欢
    • 2019-01-17
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 2019-03-31
    相关资源
    最近更新 更多