0xRLA
0xRLA

Reputation: 3369

UICollectionViewDataSource cellForItemAt don't run at iOS 14

The cellForItemAt dont't get called after I updated the build target to iOS 14 in Xcode. However, numberOfItemsInSection is getting called but cellForItemAt don't which makes it weird. I have never seen this before.

Any idea of what it could be?

extension LoginTableCell: UICollectionViewDelegate, UICollectionViewDataSource {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.cells.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LoginCollectionCell", 
    return cell
}

Upvotes: 0

Views: 276

Answers (1)

Owen
Owen

Reputation: 889

I can't see anywhere where you set the cell sizes so be sure your cells content sizes are being setup correctly. If you're using a flow layout you can set the size like this:

let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.itemSize = CGSize(width: 50, height: 50)
collectionView.collectionViewLayout = layout

Alternatively you can use the UICollectionViewDelegateFlowLayout protocol to return a size for the cell:

extension LoginTableCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.cells.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LoginCollectionCell", 
    return cell
}

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

Upvotes: 2

Related Questions