UICollectionView's cellForItemAtIndexPath is not being called

后端 未结 30 2248
再見小時候
再見小時候 2020-12-07 11:11

Only my second time using UICollectionView\'s and perhaps I have bitten off more than I can chew but nevertheless:

I am implementing a UICollectionView (myCollection

相关标签:
30条回答
  • 2020-12-07 11:26

    In my case, what I had very stupidly forgotten to do was implement the UICollectionViewDataSource protocol method numberOfItemsInSection, so that is another thing to check.

    0 讨论(0)
  • 2020-12-07 11:26

    Same happened to me. I had 2 UICollectionViews and I removed once since I didn't need that. After that I realised that the CellForItemAtIndexPath was not getting called but the other required methods. I tried all of the above but then I did the standard magic. Removed the collection view from storyboard and added again. Then it started working. Not sure why and I have no explanation but maybe some connection issues.

    0 讨论(0)
  • 2020-12-07 11:27

    It can be realated layout problems. Check layout warnings from console, if exist.

    0 讨论(0)
  • 2020-12-07 11:29

    If your class is subclass from UICollectionviewController and you are creating collectionView programmatically then use

    let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .Vertical
        layout.itemSize = CGSizeMake(50, 50)
    
        collectionView?.setCollectionViewLayout(layout, animated: false)
    
    0 讨论(0)
  • 2020-12-07 11:29

    Also, make sure that reloadData is not called while collection view is updating with animation (insert, delete, update or performBatchUpdates).

    0 讨论(0)
  • 2020-12-07 11:29

    If you're using collection view protocols you must connect the CollectionViewDataSource and CollectionViewDelegate protocols to your ViewController.

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