I have a collectionView in which I am trying to create a grid style layout. I'm using a flowLayout and standard UICollectionViewCell's for now, but I can't seem to get the collectionView to put multiple items on the same line, as I believe the flowLayout should do by default. Here is my collectionView code:
View Controller initialization code:
override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .Horizontal
collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout) super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
collectionView.backgroundColor = UIColor.whiteColor()
}
View Did Load:
collectionView.frame = self.view.bounds
collectionView.reloadData()
self.view.addSubview(collectionView)
Delegate and Data Source:
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: 40, height: 40)
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as UICollectionViewCell
func getRandomColor() -> UIColor{
var randomRed:CGFloat = CGFloat(drand48())
var randomGreen:CGFloat = CGFloat(drand48())
var randomBlue:CGFloat = CGFloat(drand48())
return UIColor(red: randomRed, green: randomGreen, blue: randomBlue, alpha: 1.0)
}
cell.backgroundColor = getRandomColor()
return cell
}
And this is what I get:
Shouldn't the cells build sideways until the edge of the collectionView's frame is hit, and then go to a new line? Any idea why this isn't happening?
Thanks,