UICollectionView's cellForItemAtIndexPath is not being called

后端 未结 30 2252
再見小時候
再見小時候 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:37

    Just resolved this issue, for a somewhat specific situation.

    In my case, I was manually initializing the UICollectionViewController in a parent ViewController, and then adding the UICollectionViewController's view as a subview.

    I resolved the issue by calling 'setNeedsLayout' and/or 'setNeedsDisplay' on the collectionView in the parent's viewDidAppear:

    - (void)viewDidAppear:(BOOL)animated {
       [super viewDidAppear:animated];
       [_collection.view setNeedsLayout];
       [_collection.view setNeedsDisplay];
    }
    
    0 讨论(0)
  • 2020-12-07 11:38

    In my case, set collectionView.prefetchingEnabled = NO; solved my problem. Only works on iOS 10.

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

    I forgot to set the autoresizing mask to false on the collection view:

    collectionView.translatesAutoresizingMaskIntoConstraints = false
    
    0 讨论(0)
  • 2020-12-07 11:40

    For a more complicated view hierachy please check this blog. It saved my life!

    self.automaticallyAdjustsScrollViewInsets = NO;
    
    0 讨论(0)
  • 2020-12-07 11:40

    In Xcode 7.0.1, we got this problem when we copied a storyboard and accompanying code from another project. The collection view had a custom flow layout set in the storyboard. Solution was to:

    • Remove the flow layout in IB
    • Compile/run the app
    • Set the flow layout back

    Now it worked :)

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

    For those who stumble here later.... the reason:

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    

    was not being called was because of the itemSize for the collectionViewFlowLayout's height was too big.

    [self.myCollectionViewFlowLayout setItemSize:CGSizeMake(320, 548)];
    

    If I change the height to 410, it will execute cellForItemAtIndexPath.

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