CollectionView sizeForItemAtIndexPath never called

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

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

class PhrasesCompactCollectionViewController: UICollectionViewController
         


        
15条回答
  •  一生所求
    2020-12-24 11:57

    Swift 5

    All these steps are required :

    1. Conform to UICollectionViewDelegateFlowLayout <== Absolutely needed !!
    2. and to UICollectionViewDelegate, UICollectionViewDataSource
    3. Set these in viewDidLoad : collectionView.delegate = self and collectionView.dataSource = self
    4. Set Estimate Size to None in the Interface Builder
    5. Make sure the following method has sizeForItemAt and not sizeForItemAtIndexPath

    as :

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

提交回复
热议问题