Rohan Vasishth
Rohan Vasishth

Reputation: 179

Collection View Cells Not Showing

I understand there are many questions answering this but my situation is slightly different. Most of the answers tell me to remove this line of code if you are using storyboards:

collectionView?.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)

But I can't do that because I am using both storyboards and and non storyboards.

I transition to this collection view controller (non-storyboard view) from a programmatic push view controller (storyboard controller- tab bar controller) like this:

func showChatLogController(user: User) {
    let chatLogController = ChatLogController(collectionViewLayout: UICollectionViewLayout())
    chatLogController.user = user
    chatLogController.hidesBottomBarWhenPushed = true
    navigationController?.pushViewController(chatLogController, animated: true)

}

My collection view code seems to be write and I have no clue what the problem is. If you need to see the collection view code here it is:

let cellId = "cellId"

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView?.backgroundColor = UIColor.whiteColor()
    collectionView?.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)

    setUpInputComponents()

    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(SignUpViewController.dismissKeyboard))
    view.addGestureRecognizer(tap)
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 5
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId, forIndexPath: indexPath)

    cell.backgroundColor = UIColor.blueColor()

    return cell
}

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

For some reason it does not display the cells. I have no clue why. Any help would be appreciated.

PS: I don't know if this is important but to get to the collection view controller I am coming from a tab bar controller.

Upvotes: 3

Views: 2459

Answers (3)

Brandon Nguyen
Brandon Nguyen

Reputation: 1

I was encountering this issue recently and found that I was instantiating the Collection View controller incorrectly. I was previously instantiating it as:

let CollectionViewController = CollectionViewController(collectionViewLayout: UICollectionViewLayout())

When it should be:

let CollectionViewController = CollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())

Upvotes: 0

charmingToad
charmingToad

Reputation: 1597

  • Is cellForRowAtIndexPath getting called? If you print something in it does it get logged?
  • Is your collection view showing up where you expect? If you set its background color do you see it?
  • Print the size you're returning in sizeForItemAtIndexPath. Is it what you expect or is a 0 sneaking in there?

Upvotes: 0

Ketan Parmar
Ketan Parmar

Reputation: 27428

set your collection view's datasource and delegate to self in your viewDidload like,

  yourCollectionView.dataSource = self
  yourCollectionView.delegate = self

Upvotes: 1

Related Questions