【发布时间】: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