contentSize is not updated after reloadData is called on UICollectionView

IosUicollectionview

Ios Problem Overview


Does anyone know why contentSize is not updated immediately after reloadData is called on UICollectionView?

If you need to know the contentSize the best work around I've found is the following:

[_collectionView reloadData];

double delayInSeconds = 0.0001;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), ^(void)
    {
        // TODO: Whatever it is you want to do now that you know the contentSize.
    });

Obviously this is a fairly brittle hack that makes assumptions on Apple's implementation but so far it has proven to work quite reliably.

Does anyone have any other workarounds or knowledge on why this happens? I'm debating submitting a radar because this I can't understand why they cannot calculate the contentSize in the same run loop. That is how UITableView has worked for its entire implementation.

EDIT: This question use to reference the setContentOffset method inside the block because I want to scroll the collection view in my app. I've removed the method call because peoples' answers focused on why wasn't I using scrollToItemAtIndexPath inside of why contentSize is not being updated.

Ios Solutions


Solution 1 - Ios

To get the content size after reload, try to call collectionViewContentSize of the layout object. It works for me.

Solution 2 - Ios

This worked for me:

[self.collectionView.collectionViewLayout invalidateLayout];
[self.collectionView.collectionViewLayout prepareLayout];

Solution 3 - Ios

Edit: I've just tested this and indeed, when the data changes, my original solution will crash with the following:

"Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (7) must be equal to the number of items contained in that section before the update (100), plus or minus the number of items inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out)."

The right way to handle this is to calculate the insertions, deletions, and moves whenever the data source changes and to use performBatchUpdates around them when it does. For example, if two items are added to the end of an array which is the data source, this would be the code:

NSArray *indexPaths = @[indexPath1, indexPath2];
[self.collectionView performBatchUpdates:^() 
{
    [self.collectionView insertItemsAtIndexPaths:indexPaths];
} completion:^(BOOL finished) {
    // TODO: Whatever it is you want to do now that you know the contentSize.
}];

Below is the edited solution of my original answer provided by Kernix which I don't think is guaranteed to work.

Try performBatchUpdates:completion: on UICollectionView. You should have access to the updated properties in the completion block. It would look like this:

[self.collectionView reloadData];
[self.collectionView performBatchUpdates:^() 
{
    
} completion:^(BOOL finished) {
    // TODO: Whatever it is you want to do now that you know the contentSize.
}];

Solution 4 - Ios

After calling

[self.collectionView reloadData];

use this:

[self.collectionView setNeedsLayout];
[self.collectionView layoutIfNeeded];

then you can get the real contentSize

Solution 5 - Ios

Call prepareLayout first, and then you will get correct contentSize:

[self.collectionView.collectionViewLayout prepareLayout];

Solution 6 - Ios

In my case, I had a custom layout class that inherits UICollectionViewFlowLayout and set it only in the Interface Builder. I accidentally removed the class from the project but Xcode didn't give me any error, and the contentSize of the collection view returned always zero.

I put back the class and it started working again.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionReid MainView Question on Stackoverflow
Solution 1 - IosClement TView Answer on Stackoverflow
Solution 2 - IosalmasView Answer on Stackoverflow
Solution 3 - IosbrodneyView Answer on Stackoverflow
Solution 4 - IosBen ShabatView Answer on Stackoverflow
Solution 5 - IosRobert MaoView Answer on Stackoverflow
Solution 6 - Iosda1View Answer on Stackoverflow