TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: resetting index of nested collectionView

Forums > Swift

I have a vertical collectionView and inside each cell, I have a View (i have other elements in the cell too) and inside the view is another collectionView (horizontal). I noticed that if I scroll the horizontal collectionView then scroll down, when the vertical cell is reused, the index of the horizontal collectionView is not at the beginning but where I stopped scrolling the first time. I tried looking at prepare for reuse and I'm resetting the elements but how can I reset the index? Or is this a delegate problem? Would appreciate any help here. Thanks.

2      

Hi,

that is quite strange behavior. Are you sure you are properly configuring the nested collectionView? Because if you change its datasource, then it shouldn't keep the old position.

If you can support iOS 13+, I would drop this approach and use compositional layout, which will help you achieve this without nested collection views.

2      

Unfortunately, we are supporting iOS12 so I cant make use of the new approach yet.

I found a solution. Basically setting the setFocusedIndex of the horizontal cell then calling that again in the outer (vertical cell) then in the Viewcontroller check if the indexPath has that item, if so, assign the current indexPath (of the horizontal cell), if not, assign an indexpath of 0.

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.