UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.