Reputation: 1268
Non of the other question on this topic have worked for me so here goes this one.
No cells are appearing despite, I am programmatically presenting the UICollectionViewController
from a UIViewController
that is in Storyboard
Note: @IBAction
segue is just a button action
This is the UIViewController
:
@IBAction func segue(sender: AnyObject) {
let collectionview = CollectionViewController(collectionViewLayout: UICollectionViewLayout())
self.presentViewController(collectionview, animated: true, completion: nil)
}
Then this is the UICollectionViewController
:
import UIKit
private let reuseIdentifier = "Cell"
class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.backgroundColor = UIColor.brownColor()
self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
// MARK: UICollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)
cell.backgroundColor = UIColor.blueColor()
return cell
}
}
I must be missing something obvious. Thanks in advance to the person who points it out for me.
Upvotes: 1
Views: 2695
Reputation: 1268
I figured out the problem, a bit silly really. When presenting the new UICollectionViewController
I initialized it wrong. I used UICollectionViewLayout
when I needed to use UICollectionViewFlowLayout
@IBAction func segue(sender: AnyObject) {
let collectionview = CollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())
self.presentViewController(collectionview, animated: true, completion: nil)
}
Upvotes: 9
Reputation: 236
So I took a jab at it and got this to work:
In your viewDidLoad() method, try this:
let screenSize = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width - 25
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
layout.itemSize = CGSize(width: screenWidth/4, height: screenWidth/4)
layout.minimumInteritemSpacing = 5
layout.minimumLineSpacing = 5
self.collectionView = UICollectionView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height), collectionViewLayout: layout)
self.collectionView?.backgroundColor = UIColor.brownColor()
self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
I guess the issue lay in not specifying a UICollectionViewFlowLayout for the cells. This adds 5px spacing between each cell but you can change that if you want. Hope this helps!
Upvotes: 1