Cells disappearing from UICollectionView… I'm losing my mind!

Hey everyone ! I don’t know if I’m at the right place to ask but, but let me know if it’s not. My heart is pure, so if I’m in the wrong place, please excuse me. Anyway.

My cells are disappearing once I tap on them :

enter image description here

Here’s the code :

import UIKit

class A26ViewController:  UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

let cellId = "pouet"

lazy var topCollectionView : UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
    view.collectionViewLayout = layout
    layout.minimumLineSpacing = 0
    view.dataSource = self
    view.delegate = self
    view.translatesAutoresizingMaskIntoConstraints = false
    view.backgroundColor = .red
    return view
}()

override func viewDidLoad() {
    view.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(topCollectionView)
    topCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
    topCollectionView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    topCollectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    topCollectionView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    topCollectionView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.size.width).isActive = true
    print(view.frame.size)
    view.backgroundColor = .blue
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

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

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = topCollectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
    cell.backgroundColor = UIColor(red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 1)
    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print(indexPath.item)
}

override func viewDidLayoutSubviews() {

}

}

My guess is that it’s related to the start of the scrolling, but I’m not sure… The contentSize looks fine after printing it in the didLayoutSubviews. Strange, isn’t it? But this bug is going to send me to the hospital…

edit: OK so it’s not related to the scroll. When I tap on the red border, which aren’t the cells, they don’t disappear and I can scroll without making any screening bug. But in the same time, when I tap, I don’t execute didSelectItem (the print indexPath.item thing). Really, it’s insane.

Hi @petaire, without the full code it’s difficult to work out what is going on however I see a few things that could potentially be causing you issues here:

  • Missing call to super in viewDidLayoutSubviews
  • Missing call to super in viewDidLoad
  • Setting translatesAutresizingMasksIntoConstraints on view is not nessacery (usually)
  • You don’t have to set the collectionView’s layout property after already initialising it with the same layout
  • You could pin the rightAnchor of the collectionView instead of the widthAnchor
  • I think you actually want a hozizontal layout direction

However I don’t think they are the cause of your problem. I’ve put the above code into a playground (attached) with a few tweaks that i mentioned above and everything seems to be working fine. Collection View Test.playground.zip (7.4 KB)

Is this view controller nested within another view controlller? If so how is that happening? I think this code in isolation isn’t the root cause of your problem so if you could maybe talk us through how you are setting up the entire view then that would be useful :blush:

Hi,
view.translatesAutoresizingMaskIntoConstraints = false, why you are setting this.

topCollectionView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.size.width).isActive = true

Not a good idea to use width anchor, instead better choice is to use, rightAnchor, or equal widths constraint.

override func viewDidLayoutSubviews() {

}

Why overriding this method, and not even calling super ?
Check for your cell code too, and looks like issue could be your overall setup, first see if your collection view works well in a single view controller, with only collection view. If it works then looks like problem is with your overall setup, care to share more details on that.