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 :
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.