Zerium
Zerium

Reputation: 17333

Collection View Cells not appearing in Collection View?

I created a Collection View using purely the storyboard interface builder. This is what the storyboard looks like:

Storyboard

My collection view's properties are default as well. I haven't written anything into my ViewController.swift yet.

Collection View

For some reason, when I run on my phone / emulator, none of the buttons are showing.

Upvotes: 0

Views: 5424

Answers (2)

vaibhav
vaibhav

Reputation: 4096

Just configure the collectionView properly see below code and image:

Implement the delegate methods of collectionView:

class yourClassController: UICollectionViewDataSource, UICollectionViewDelegate {

override func numberOfSectionsInCollectionView(collectionView: 
    UICollectionView!) -> Int {
    return 1
}

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

override func collectionView(collectionView: UICollectionView!, 
cellForItemAtIndexPath indexPath: NSIndexPath!) -> 
     UICollectionViewCell! {

     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as CollectionViewCell

     // Configure the cell
     cell.backgroundColor = UIColor.blackColor()
     cell.textLabel?.text = "\(indexPath.section):\(indexPath.row)"
     cell.imageView?.image = UIImage(named: "circle")



    return cell
}

Then from your storyboard set the delegate and datasource by drag and drop see image:

enter image description here

Note: collectionView appears when you do complete above formality with its relevant class.

Upvotes: 3

nishith Singh
nishith Singh

Reputation: 2998

UICollectionView does not support static cells like UITableView. You will have to set its dataSource,delegate and configure your cells in code.

Upvotes: 4

Related Questions