2016-04-28 16 views
0

シンプルなコレクションビューを作成し、クリックすると各セルが表示される画面のプレビューを表示するようにセルを設定しました。これはすべてうまくいきましたので、私は各セクションにヘッダーを追加するつもりです。私はそれがcollectionViewが消える(あるいは少なくともそれが白い画面以外に何も表示されません)原因コードにセクションを追加しましたセクションヘッダーを追加した後Swift CollectionViewが表示されなくなりました

しかし、ここで問題がある...

Blank Screen

ここで私が追加(または変更)コードがあり、

override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { 

     let sectionHeaderView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "SectionHeader", forIndexPath: indexPath) as! SectionHeaderView 
     if let title = papersDataSource.titleForSectionAtIndexPath(indexPath) { 
      sectionHeaderView.title = title 
     } 
     return sectionHeaderView 
    } 

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
     return papersDataSource.numberOfSections 
    } 

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return papersDataSource.numberOfPapersInSection(section) 
    } 
...そこコード掲載のに十分ではありません、私はより多くを掲載する予定なら、私に知らせてください

そして、私は次のコードを持っているUICollectionReusableViewのサブクラスを設定...

import UIKit 

class SectionHeaderView: UICollectionReusableView { 

    @IBOutlet weak var titleLabel: UILabel! 

    var title: String? { 
     didSet { 
      titleLabel.text = title 
     } 
    } 
} 

すべては罰金コンパイルが、完全に空白表示されます。

どのような考えですか?

+0

はリンゴのドキュメントを読むと、あなたがこの 'UICollectionViewLayout'メソッドをオーバーライドする必要がありそうです。私はメソッドが返すべきだと思う: '.SupplementaryView' – alaphao

答えて

0

上記のすべてのコードが正しいと判断したら、すべてのデータがセットアップされてロードされているdataSourceファイルをよく見てみることにしました。周りの狩猟とき私が問題を引き起こしていたものを発見し、それがディスクにデータをロードする方法でした...

private func loadPapersFromDisk() -> [Paper] { 
    sections.removeAll(keepCapacity: false) 
    if let path = NSBundle.mainBundle().pathForResource("Papers", ofType: "plist") { 
     if let dictArray = NSArray(contentsOfFile: path) { 
     var papers: [Paper] = [] 
     for item in dictArray { 
      if let dict = item as? NSDictionary { 
      let caption = dict["caption"] as! String 
      let imageName = dict["imageName"] as! String 
      let section = dict["section"] as! String 
      let index = dict["index"] as! Int 
      let paper = Paper(caption: caption, imageName: imageName, section: section, index: index) 
      if !sections.contains(section) { 
       sections.append(section) 
      } 
      papers.append(paper) 
      } 
     } 
     return papers 
     } 
    } 
    return [] 
    } 

具体的には、問題がセクションに追加の論文にありました。 `layoutAttributesForSupplementaryViewOfKind:atIndexPath:`それはのように見えた前のセクションでは...、アンラップされなければならなかった

if sections.contains(section) { 
    sections.append(section) 
} 
関連する問題