2016-09-19 116 views
1

我使用的是RxSwift,我的UIViewController包含UICollectionView。我已經在努力將頭添加到我的收藏看法,但這個不會被調用:RxSwift和UICollectionView標頭

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) 

回答

-1

它無關,跟RxSwift除非你使用RxDataSources,那是。

你只是沒有正確設置你的UICollectionView。其中包括:

  • 確保您定義了func numberOfSections(in collectionView: UICollectionView) -> Int以返回大於0的內容。
  • 確保您定義func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize返回的尺寸大於CGSize(width: 0, height: 0)
  • 如果您使用的是nib,請將其註冊爲func register(_ nib: UINib?, forSupplementaryViewOfKind kind: String, withReuseIdentifier identifier: String)。或者,如果您以編程方式創建它,請將其註冊爲func register(_ viewClass: AnyClass?, forSupplementaryViewOfKind elementKind: String, withReuseIdentifier identifier: String)
+0

我已經試過您的解決方案,它並沒有幫助。我的假設是,在UICollectionViewDataSource協議中定義了func collectionView(collectionView:UICollectionView,viewForSupplementaryElementOfKind kind:String,atIndexPath indexPath:NSIndexPath),它由RxSwift實現。它可能是什麼?也許我應該用更多的信息更新我的問題? – alexxjk

+0

我不認爲添加更多信息會有幫助,因爲您基本上必須交出所有代碼。我建議查看一下'UICollectionView'教程,並嚴格按照它來確保你獲得了一些基礎工作。然後從那裏修改它。 – solidcell

1

我有同樣的問題。解決它移動到RxCollectionViewSectionedReloadDataSource

這種類型的數據源有頁眉/頁腳。標準RxCollectionViewReactiveArrayDataSource沒有頁眉/頁腳。

即使在RxCocoa UICollectionView+Rx擴展中,也以RxCollectionViewSectionedReloadDataSource爲例。

例子:

 let dataSource = RxCollectionViewSectionedReloadDataSource<SectionModel<String, Double>>() 

    let items = Observable.just([ 
     SectionModel(model: "First section", items: [ 
      1.0, 
      2.0, 
      3.0 
     ]), 
     SectionModel(model: "Second section", items: [ 
      1.0, 
      2.0, 
      3.0 
     ]), 
     SectionModel(model: "Third section", items: [ 
      1.0, 
      2.0, 
      3.0 
     ]) 
    ]) 

    dataSource.configureCell = { (dataSource, cv, indexPath, element) in 
     let cell = cv.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! NumberCell 
     cell.value?.text = "\(element) @ row \(indexPath.row)" 
     return cell 
    } 

    items 
     .bind(to: collectionView.rx.items(dataSource: dataSource)) 
     .disposed(by: disposeBag) 
+0

歡迎來到SO。請格式化您的答案,這裏是您需要做的信息的鏈接https://meta.stackexchange.com/help/formatting –