CollectionView sizeForItemAtIndexPath never called

前端 未结 15 1478
一整个雨季
一整个雨季 2020-12-24 11:17

This is driving me crazy! I have a UICollectionViewController as shown below:

class PhrasesCompactCollectionViewController: UICollectionViewController
         


        
15条回答
  •  被撕碎了的回忆
    2020-12-24 12:03

    I had the same problem and nothing would fix it. I had a yellow warning saying to make it private because it came close to another function defined by the Layout Delegate. What fixed it for me was:

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    

    Notice the difference from "sizeForItemAtIndexPath" to the correct "sizeForItemAt".

    I tore my hair out for days trying to figure this out and it finally worked. Below I will include all of my function to show you how I also "hid" a cell by making the height equal to 0.

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let findIndex = labelArray.index(of: "\(labelArray[indexPath.row])")
        let screenSize = UIScreen.main.bounds
        let screenWidth = screenSize.width
    
        if (findIndex! % 2 == 0){
            // Even index, show cell
            return CGSize(width: screenWidth, height: 246)
        }
        else {
            return CGSize(width: screenWidth, height: 0)
        }
    }
    

提交回复
热议问题