CollectionView sizeForItemAtIndexPath never called

前端 未结 15 1263
一整个雨季
一整个雨季 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:04

    In Swift 3+ Use this method:

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

    Make sure that your class complies both Delegate

    • UICollectionViewDelegate

    • UICollectionViewDelegateFlowLayout

    0 讨论(0)
  • 2020-12-24 12:04

    For me (in Swift 3.1) the method must be declared public like this:

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

    Don't forget to make it public.

    0 讨论(0)
  • 2020-12-24 12:05

    my problem was that I had my UICollectionViewDataSource object created as local variable. So if you initialize your dataSource object manually in code, make sure to store it as global variable.

    0 讨论(0)
提交回复
热议问题