【问题标题】:UICollectionView viewForSupplementaryElementOfKind not called with Firebase UI dataSourceUICollectionView viewForSupplementaryElementOfKind 未使用 Firebase UI 数据源调用
【发布时间】:2017-12-02 15:47:09
【问题描述】:

我正在尝试将标题添加到绑定到使用 FirebaseUI 的 firebase 查询的集合视图中。

我使用集合标题的静态框架设置我的集合视图。 (我尝试了一个集合视图,其中调用sizeForHeaderInSection 的委托而不是布局中设置的静态框架。无论如何都不会调用viewForSupplementaryElementOfKind 的委托方法。):

lazy var collectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.headerReferenceSize = CGSize(width: CGFloat(UIScreen.main.bounds.width), height: 100)
    let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
    view.contentInset = UIEdgeInsetsMake(84, 0, 0, 0)
    view.register(VideoPollCollectionViewCell.self, forCellWithReuseIdentifier: NSStringFromClass(VideoPollCollectionViewCell.self))
    view.register(PollCollectionViewHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: NSStringFromClass(PollCollectionViewHeader.self))
    view.translatesAutoresizingMaskIntoConstraints = false
    view.delegate = self
    view.dataSource = self
    view.tag = VideoPollCollectionType.polls.rawValue
    return view
}()

viewDidLoad()中配置dataSource

    self.dataSource = self.collectionView.bind(to: self.videoPollQuery, populateCell: { (collectionView, indexPath, snap) -> UICollectionViewCell in
        let videoPoll = VideoPoll(with: snap)
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(VideoPollCollectionViewCell.self), for: indexPath) as? VideoPollCollectionViewCell else { return UICollectionViewCell() }
        cell.setPoll(with: videoPoll)
        return cell
    })

在实例属性级别添加 FirebaseUI 的数据源:

lazy var videoPollQuery: DatabaseQuery = {
    let ref = Database.database().reference(withPath: "/polls/")
    return ref.queryOrderedByKey()
}()

var dataSource: FUICollectionViewDataSource!

并添加委托/数据源扩展,希望加载集合标题视图的方法被调用

extension PollCollectionViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        guard let view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: NSStringFromClass(PollCollectionViewHeader.self), for: indexPath) as? PollCollectionViewHeader else { return UICollectionReusableView() }
        return view
    }
}

这会生成一个集合视图,其中包含在集合视图初始化程序或 sizeForHeaderInSection 委托方法的布局中设置的静态帧大小的空标题。

viewForSupplementaryElementOfKind 中添加断点后,我很困惑地发现这个函数没有被调用。

有谁知道将UICollectionReusableView 的子类作为标题添加到带有来自 Firebase 的数据源的集合视图的正确过程?

【问题讨论】:

    标签: ios swift firebase uicollectionview firebaseui


    【解决方案1】:

    尝试定义补充视图的高度:

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
            return CGSize(width:collectionView.frame.size.width, height:30)
    }
    

    【讨论】:

    【解决方案2】:

    确保当您初始化 CollectionVC 时,布局参数是 UICollectionViewFlowLayout() 而不是 UICollectionViewLayout() 并且您的 collectionVC 子类为 UICollectionViewDelegateFlowLayout

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-17
      • 2018-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-23
      • 1970-01-01
      相关资源
      最近更新 更多