0

I've got this simple code to control Collection view, but it doesn't show any cells. The app works, but it's blank. The identifier match with cell identifier and the class is attached to the collection view. I think the problem is with upcast: as! UICollectionViewCell

class CollectionViewController: UICollectionViewController {
     var Array = ["one", "two"]

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
         super.didReceiveMemoryWarning()

    }
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
          return Array.count
   }

   override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! UICollectionViewCell
        return cell
   }
}
Amit89
  • 3,000
  • 1
  • 19
  • 27
redike
  • 15
  • 2
  • 6

2 Answers2

0

may be you forgot to check the as initial view controller option from storyboard...

enter image description here

In the right side panel under View Controller just below the title, there is a checkbox as Initial View controller, tick this and then try to run...

Himanshu gupta
  • 650
  • 5
  • 10
-1

Make sure your overriding of numberOfSectionsInCollectionView: is not returning 0.

Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206
  • Well, he returns `Array.count` which 2, but he does not fill in the cell. – qwerty_so Jun 02 '15 at 18:07
  • ["If you do not implement this method, the collection view uses a default value of 1."](https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UICollectionViewDataSource_protocol/index.html#//apple_ref/occ/intfm/UICollectionViewDataSource/numberOfSectionsInCollectionView:) – Brian Nickel Jun 02 '15 at 18:08
  • @ThomasKilian he's returning Array.count on items in section, not in sections in collection. – Christopher Francisco Jun 02 '15 at 18:10
  • @Brian, I know, but he might had overriden the method (boilerplate code from xcode?) and is returning 0, he just didnt post it here – Christopher Francisco Jun 02 '15 at 18:10
  • even if I return 1 section it still doesn't show the cells – redike Jun 02 '15 at 18:13