2017-06-29 65 views
0

我正在嘗試向使用FirebaseUI綁定到Firebase查詢的集合視圖添加標頭。UICollectionView viewForSupplementaryElementOfKind未使用Firebase UI數據調用來源

我使用靜態框架爲集合標題設置了我的集合視圖。 (我試圖與一個代表呼叫的集合視圖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()

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內添加一個斷點後,我很困惑發現這個函數沒有被調用。

有誰知道使用Firebase中的數據源將一個UICollectionReusableView的子類作爲頭添加到集合視圖的正確過程嗎?

回答

0

嘗試定義高度補充視圖:

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

添加此不會導致'viewForSupplementaryElementOfKind'被調用。 – thexande

相關問題