FirebaseUIを使用してfirebaseクエリにバインドされたコレクションビューにヘッダーを追加しようとしています。UICollectionView viewForSupplementaryElementOfKindがFirebase UIのdataSourceで呼び出されていません
コレクションヘッダーの静的フレームを使用してコレクションビューを設定しました。 (私の代わりに、レイアウトに設定された静的フレームの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
}()
設定のdataSource viewDidLoad()
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のデータソースを持つコレクションビューのヘッダーとして追加する正しい手順を知っている人はいませんか?
これは 'viewForSupplementaryElementOfKind'が呼び出されることはありません追加します。 – thexande