2016-04-28 59 views
0

我創建了一個簡單的collectionView,並設置單元格顯示每個單元格單擊時要顯示的屏幕預覽。這一切工作正常,所以我正在向每個部分添加標題。Swift CollectionView在添加節標題後停止顯示

然而,現在的問題是...

當我加入了部分的代碼,所以造成的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'方法:'layoutAttributesForSupplementaryViewOfKind:atIndexPath:'。我認爲該方法應該返回:'.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 [] 
    } 

具體來說,問題是在所附論文的部分。該部分必須拆開,它看起來像以前一樣......

if sections.contains(section) { 
    sections.append(section) 
} 
相關問題