0

I have created a simple collectionView and set the cells to show a preview of the screen each cell goes to when clicked. This all worked fine and so I am moving forward to adding headers to each of the sections.

However, here is the problem...

When I added the sections to the code it caused the collectionView to disappear (or at least it doesn't show anything else besides a white screen)

Blank Screen

Here is the code I added (or modified), please let me know if there isn't enough of the code posted and I will post more...

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)
    }

And I set up a sub-class of UICollectionReusableView which has the following code...

import UIKit

class SectionHeaderView: UICollectionReusableView {

    @IBOutlet weak var titleLabel: UILabel!

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

Everything compiles fine but shows up completely blank.

Any thoughts?

Robert Byrne
  • 907
  • 1
  • 9
  • 18
  • Reading the apple docs, it seems you have to override this `UICollectionViewLayout` method: `layoutAttributesForSupplementaryViewOfKind:atIndexPath: `. I think the method should return: `.SupplementaryView` – alephao Apr 28 '16 at 01:51

1 Answers1

0

After deciding that all the code posted above was correct I decided to have a better look at the dataSource file where all of the data is setup and loaded. When hunting around I found what was causing the problem and it was the method that loaded the data to disc...

  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 []
  }

Specifically, the problem was in the appending papers to a section. The sections had to be unwrapped, before it looked like...

if sections.contains(section) {
  sections.append(section)
}
Robert Byrne
  • 907
  • 1
  • 9
  • 18