Search code examples
iosobjective-cdynamicuicollectionviewcgsize

Dynamic sized collectionView cell causing visibility issue - iOS


I have a 3 columns and multiple rows Collection View. In my sizeForItemAtIndexPath, I return a dynamic CGSize like this :

return CGSizeMake(_sponsoredCollectionView.frame.size.width/3 - 7, (_sponsoredCollectionView.frame.size.width/3 - 7 + nameHeight + cuisineHeight + addressHeight + timingHeight + paddings));

But with the above size, my last cell is not being displayed ( I don't understand why, because the height that is being returned is similar to previous cells and there is a lot of space for it to be shown, as previous cells did), see screenshot below : enter image description here

And when I hardcode the size to the following :

return CGSizeMake(_sponsoredCollectionView.frame.size.width/3 - 7,250);

It is shown (cell with KEKOU title, but obviously with a lot of empty spaces above and below due to hardcode 250 height, see screenshot below : enter image description here

Also, I am doing the following, for the CollectionView to resize according to cells after realoading

[_sponsoredCollectionView reloadData];

dispatch_async(dispatch_get_main_queue(), ^{
  _sponsoredCollectionViewHeightConstraint.constant = _sponsoredCollectionView.collectionViewLayout.collectionViewContentSize.height + 0;
});

I am unable to understand what is the issue :S Please Help!


Solution

  • Finally after one full night of trial and error and research, I solved it by just changing the _sponsoredCollectionViewHeightConstraint.constant inside a performBatchUpdates like this :

    [_sponsoredCollectionView reloadData];
    
    dispatch_async(dispatch_get_main_queue(), ^{
                            [self.sponsoredCollectionView performBatchUpdates:^{
                                _sponsoredCollectionViewHeightConstraint.constant = _sponsoredCollectionView.collectionViewLayout.collectionViewContentSize.height + 5;//_sponsoredCollectionView.contentSize.height + 5;
                            } completion:nil];
                        });