'How to remember an indexPath of a cell for reuse in the future?

I can get the current indexPath of a collectionViewCell. The method I use is this one:

var currentIndexPath: IndexPath = [0, 0]

func scrollViewDidScroll(_ scrollView: UIScrollView) {
            let visibleRect = CGRect(origin: collectionView.contentOffset, size: collectionView.bounds.size)
            let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
            if let myIndexPath = collectionView.indexPathForItem(at: visiblePoint) {
                currentIndexPath = myIndexPath
            }
        }

The question is: how can I save indexPath value, so in the future I can call it - and replace some future indexPath value with a current one? So it should work like a bookmark - you can go back to that place and continue scrolling back and forth from there.

I have tried just to save it to a var (e.x. via button tap) like this : savedIndexPath = currentIndexPath. But when I gave it a test - all elements in the collectionView started to look like clones of each other.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source