ios - 未使用 Firebase UI 数据源调用 UICollectionView viewForSupplementaryElementOfKind

标签 ios swift firebase uicollectionview firebaseui

我正在尝试将 header 添加到使用 FirebaseUI 绑定(bind)到 firebase 查询的 Collection View 。

我使用集合标题的静态框架设置我的 Collection View 。 (我已经尝试使用委托(delegate)调用 sizeForHeaderInSection 而不是布局中的静态框架集的 Collection View 。viewForSupplementaryElementOfKind 的委托(delegate)方法无论如何都不会被调用。):

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!

并添加委托(delegate)/数据源扩展,希望调用加载集合标题 View 的方法

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
    }
}

这会生成一个 Collection View ,其中包含在 Collection View 初始值设定项的布局或 sizeForHeaderInSection 委托(delegate)方法中设置的静态帧大小的空 header 。

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

有谁知道将 UICollectionReusableView 的子类作为 header 添加到具有来自 Firebase 的数据源的 Collection View 的正确过程?

最佳答案

尝试为补充 View 定义高度:

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

关于ios - 未使用 Firebase UI 数据源调用 UICollectionView viewForSupplementaryElementOfKind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44816710/

相关文章:

swift - NSAlert() runmodal 此类对于键 modalWindow 不符合键值编码

swift - UIButton 的 textLabel 未被剪切

java - 添加 firebase 与 google play 服务冲突

javascript - Firebase getDownloadURL() 不返回任何内容

ios - AFNetworking - 对 REST 进行 HTTP POST,发送带有 Base64 字符串图像的 JSON

iOS:外部显示器 AVPlayerController 关闭问题

ios - 为什么我无法同时创建数据并将数据添加到 sqlite 文件?

ios - Unity/iOS - MoPub 安装 - Xcode 编译错误

iOS App Rejection 由于 2.23 - Realm 数据库

Firebase Pub/Sub 触发器 : executing multiple times sporadically