2017-06-29 3 views
0

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のデータソースを持つコレクションビューのヘッダーとして追加する正しい手順を知っている人はいませんか?

答えて

0

は、補助ビューの高さを定義してください:

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

これは 'viewForSupplementaryElementOfKind'が呼び出されることはありません追加します。 – thexande

関連する問題