未使用Firebase UI dataSource调用UICollectionView viewForSupplementaryElementOfKind

时间:2017-06-29 05:03:44

标签: ios swift firebase uicollectionview firebaseui

我正在尝试使用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数据源的集合视图的正确过程?

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

请确保在初始化CollectionVC时,布局参数为launchURL() async { static const String homeLat = "37.3230"; static const String homeLng = "-122.0312"; static final String googleMapslocationUrl = "https://www.google.com/maps/search/?api=1&query=${TextStrings.homeLat},${TextStrings.homeLng}"; final String encodedURl = Uri.encodeFull(googleMapslocationUrl); if (await canLaunch(encodedURl)) { await launch(encodedURl); } else { print('Could not launch $encodedURl'); throw 'Could not launch $encodedURl'; } } 而不是UICollectionViewFlowLayout(),并且确保collectionVC子类为UICollectionViewLayout()