user8188756
user8188756

Reputation:

Programmatically made UICollectionViewController not showing cells

The background is black and the cells are white though none of the cells are showing in the simulator? would any one know why this might be?

import UIKit
import Foundation

class ChatLog: UICollectionViewController, UITextFieldDelegate, UICollectionViewDelegateFlowLayout {

    let cellId = "cellId"

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView?.backgroundColor = UIColor.black
        collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)    
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
        cell.backgroundColor = UIColor.white

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: view.frame.height, height: 80)
    }

}

Upvotes: 1

Views: 3477

Answers (2)

Pranav Kasetti
Pranav Kasetti

Reputation: 9925

Update:

Initialising the ChatLog view controller programmatically as follows means that the UICollectionView datasource methods aren't called e.g collectionView(_:cellForItemAt:) isn't called.

let newViewController = ChatLog(collectionViewLayout: UICollectionViewLayout())

Replace with this:

let newViewController = ChatLog(collectionViewLayout: UICollectionViewFlowLayout())

--

As you have declared a UICollectionViewController, you don't actually need to set explicitly in code, the delegate and dataSource properties of your collection view.

Simply make sure that in the Main.storyboard, you have set the class of your UICollectionViewController to ChatLog by clicking on the view controller and then the identity inspector. Also ensure that you have clicked the UICollectionViewCell and set it's Identifier to "cellId".

If this is a multiple view controller project, ensure that it is possible to navigate to ChatLog view controller by making it initial view controller or providing segue/navigation to this view controller from another view controller.

Pictures below outline my solution.

Storyboard Cell

Storyboard UICollectionViewController ChatLog

Upvotes: 7

Aravind A R
Aravind A R

Reputation: 2714

Set the delegate and datasource of the collectionView. The datasource and delegate methods(numberOfItemsInSection, cellForItemAtIndexpath, etc) will be called only if you have set the delegate and datasource. You can set it either in code or in storyboard(if you have used storyboard to design the collectionView)

In the viewDidLoad you can add

 collectionView.delegate = self
 collectionView.datasource = self

Upvotes: 0

Related Questions