2017-09-29 6 views
0

私はUICollectionViewにいくつかのヘッダーがあります。 セクションヘッダーとして実装されています。 各ヘッダはそれぞれのコントローラに属し、コントローラはMyHeaderProtocolに準拠しています。UIViewControllerセクションのヘッダーが表示されない

基本的に私はコントローラの配列を持っていて、必要に応じてそれぞれからヘッダーセルを要求します。

ので:

  1. collectionView(:viewForSupplementaryElementOfKind:at)が呼び出され、実際のビューを返します。表示が適切に設定され、フレームのサイズが適切であるなどの理由で、色が紫色になる場合がありますが、画面に表示されません。
  2. collectionView(_:layout:referenceSizeForHeaderInSection:)が呼び出され、良好なCGSize値が返されます。

コレクションビューが表示されますが、これらすべてのヘッダーの代わりに空白の空白が表示されます。 問題は - ヘッダーセクションを表示するには両方の必要なメソッドが呼び出されますが、セクションヘッダーは表示されません(スペースは予約されています)。私は問題がカスタムMyHeaderProtocolプロトコルでかもしれないと思う

protocol MyHeaderProtocol: class { 
    var nib: UINib { get } 
    var cellReuseIdentifier: String { get } 
    // Will dequeue UICollectionViewCell using dequeueReusableSupplementaryView and set data for this cell 
    func setupHeader(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: IndexPath) -> UICollectionViewCell? 
    func getCellHeight() 
} 

class MyCollectionView: UIViewController { 
    @IBOutlet private weak var collectionView: UICollectionView 
    private var headers: [MyHeaderProtocol] = [] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     collectionView.delegate = self 
     collectionView.dataSource = self 
     // it will instantiate controllers conformed to MyHeaderProtocol and append them to header array 
     setUpHeaders() 
     registerHeaders() 
    } 

    private func setUpHeaders() { 
     let headerOne = SimpleHeader1(withModel: viewModel) 
     headers.append(headerOne) 
     let headerTwo = SimpleHeader2(withModel: viewModel) 
     headers.append(headerTwo) 
    } 

    private func registerHeaders() { 
     for header in headers { 
      collectionView.register(header.nib, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: header.cellReuseIdentifier) 
     } 
    } 

} 

extension MyCollectionView: UICollectionViewDataSource { 

    func numberOfSections(in collectionView: UICollectionView) -> Int { 
     return headers.count + 1 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     if section == headers.count { 
      // If section isn't a header section - we return actual cells 
      return viewModel.people.count 
     } else { 
      // if it is a header - we return only section, no intems 
      return 0 
     } 
    } 

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 
     var view : UICollectionReusableView? 
     switch kind { 
     case UICollectionElementKindSectionHeader: 
      // if it is a header 
      if indexPath.section < headers.count { 
       let header = headers[indexPath.section] 
       view = header.setupHeader(collectionView: collectionView, cellForItemAtIndexPath: indexPath) 
       // to test that header is visible 
       view?.backgroundColor = UIColor.purple 
      } 
     case UICollectionElementKindSectionFooter: 
      break 
     default: 
      assert(false, "Unexpected element kind") 
      break 
     } 
     return view ?? UICollectionReusableView() 
    } 
} 

extension MyCollectionView : UICollectionViewDelegateFlowLayout{ 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { 
     var size = CGSize.zero 
     // If that's header - return non-Zero size 
     if section < headers.count { 
      let header = headers[section] 
      size = CGSize.init(width: collectionView.bounds.width, height: header.getCellHeight()) 
     } 
     return size 
    } 
} 
+0

これを構築する時間を節約するには、あなたのsetupHeaders関数を共有してください。それとも、プロジェクト全体のgithubリンクを投稿してください。 –

+0

プロジェクトをGuthubに投稿することはできません。しかし、後でテストプロジェクトをセットアップする時間があるかもしれません。 'setupHeaders'を追加しますが、それは多くの助けになるとは思いません。 – OgreSwamp

+0

私はあなたが提供した情報を手助けしようとしました。しかし、あなたの問題をより良く理解するためには、テストプロジェクトが素晴らしいでしょう。 –

答えて

0

:ここ

は私のコードのバージョン簡略化されています。特に、この機能:

func setupHeader(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: IndexPath) -> UICollectionViewCell? 

あなたがここにヘッダを返す必要が、そうでない場合はこの機能collectionView(:viewForSupplementaryElementOfKind:atが期待される動作を生成しません。

var view : UICollectionReusableView? 
view = header.setupHeader(collectionView: collectionView, cellForItemAtIndexPath: indexPath) 

ので、代わりにこれを試してみてください。

func setupHeader(collectionView: UICollectionView, headerForItemAtIndexPath indexPath: IndexPath) -> UICollectionReusableView 

私はお役に立てれば。

+0

私はそれが問題だとは思わない。 LLVMはチェックタイプをチェックし、タイプの問題があれば、このメソッドから間違ったタイプを返すことはできません。実際、 'UICollectionViewCell'は' UICollectionReusableView'から継承しています。だからそれはうまくいくはずです。 'SimpleHeader1'と' SimpleHeader2'ヘッダクラスも 'UIView'クラスではありません。私が言及したように、それらはコントローラ(またはマネージャ)であり、xibから対応する 'SimpleHeader1View'クラスをインスタンス化します。 – OgreSwamp

+0

これは、ヘッダーとして 'UICollectionViewCell'を使用することが文書化されていないと思います。 –

関連する問題