Skip to content
This repository has been archived by the owner on Jun 17, 2023. It is now read-only.

issues with loaded UICollectionViewCells after reloadData() when layout.currentPage > layout.numberOfVisibleItems #26

Closed
jlynch1 opened this issue Aug 15, 2020 · 3 comments

Comments

@jlynch1
Copy link
Contributor

jlynch1 commented Aug 15, 2020

I have 10 UICollectionViewCell (each with an image inserted) in UICollectionView, and initially due to restrictions on the rate of downloading images I had reduced the number of visible items to 5
layout.numberOfVisibleItems = 5

When I go to change my data model and do a full reload of the UICollectionView, depending on what UICollectionViewCell Im on....ie the value for layout.currentPage and layout.currentScrollOffset determines which 5 cells will be loaded in memory after the reloadData()....

.e.g. currentPage is 9, do a reloadData(), and cells 5,6,7,8,9 are loaded into memory. then I setCurrentPage() to 2

for the UICollectionViewCells 0 and 1 behind page 2, the images displayed are from the recycled UICollectionViewCell taken from memory like if they were cached - even though the new images have been downloaded again and set in the imageView in the UICollectionViewCell

Initially I was thinking:

  • could be an issue between the reloadData() call and setCurrentPage() but these are both performed on the main thread.
  • I played around with forcing invalidateLayout() before and after the reloadData()
  • I was thinking if I should reset layout.currentPage before doing the reload.
DispatchQueue.main.async {
  self.carouselView.collectionViewLayout.invalidateLayout()
  self.carouselView.layoutIfNeeded()

  self.dataModel = newDataModel
  self.carouselView.reloadData()

  layout.setCurrentPage(index, animated: true)
}

But when I don't set layout.numberOfVisibleItems and leave it at the default, the issue with showing the wrong images behind the currentPage does not happen. There is also no need for invalidateLayout() here

So I am wondering if I'm missing something when using layout.numberOfVisibleItems or if there is a bug there

@amirdew
Copy link
Owner

amirdew commented Aug 16, 2020

Hi,
the way that you use numberOfVisibleItems seems okay, I think the issue is about invalidating, could you try invalidating layout via performBatchUpdates like this?

        collectionView.reloadData()
        collectionView.performBatchUpdates({
            self.collectionView.collectionViewLayout.invalidateLayout()
        })

if this didn't work, can you make a simple project and share it here, it's probably not related to downloadable images!
it might be a bug but I need to find a way to reproduce it.

@jlynch1
Copy link
Contributor Author

jlynch1 commented Aug 16, 2020

Hi Amir,

Thanks for the response, I had tried the way you suggest... the same way as its done in viewDidLayoutSubviews() method but it had the same problems. By the way I was using the configuration for the inverted cylinder which I took from the mac layout designer app. I will look at creating a sample project to reproduce the issue.

Thanks
Jonathan

@amirdew
Copy link
Owner

amirdew commented Apr 25, 2021

Hi Jonathan,
With the new version the issue should be fixed, no need to call performBatchUpdates or invalidateLayout anymore,
Check the version 1.0.0 or higher and feel free to open a new ticket if needed

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants