如何记住将来的单元格重复使用的indexpath?

发布于 2025-01-30 11:51:52 字数 742 浏览 1 评论 0原文

我可以得到当前的收集视图。我使用的方法是:

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
            }
        }

问题是:如何保存indexpath值,因此我将来可以称呼它 - 并用当前的indexpath值替换一些未来的Indexpath值?因此,它应该像书签一样工作 - 您可以回到那个地方,然后继续从那里来回滚动。

我试图将其保存到var(通过按钮点击)之类的:savedIndExpath = CurrentIndExpath。但是,当我对其进行测试时 - 收集视图中的所有元素都看起来像是彼此的克隆。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文