如何在swift中滚动到集合视图中的特定元素索引 [英] How to scroll to a particluar index in collection view in swift

查看:382
本文介绍了如何在swift中滚动到集合视图中的特定元素索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个集合视图。我做了一个日历,横向显示日期和我水平滚动日期。现在在屏幕上我只看到3-4个日期。现在我想在我显示的日历屏幕时自动滚动到特定的选定日期。所以我想要滚动到的日期还不可见。

We have a collection view. I have made a calendar which shows dates horizontally & I scroll dates horizontally. Now on screen I see only 3-4 dates. Now I want to auto scroll to a particular selected date when calendar screen I shown.So the date I want to scroll to is not visible yet.

为此,我得到了特定单元格的索引路径。现在我正在尝试将其滚动到特定的索引路径。

For that I got the indexpath for particular cell. Now I am trying to scroll it to particular indexpath.

  func scrollCollectionView(indexpath:IndexPath)
  {
   // collectionView.scrollToItem(at: indexpath, at: .left, animated: true)
    //collectionView.selectItem(at: indexpath, animated: true, scrollPosition: .left)
    collectionView.scrollToItem(at: indexpath, at: .centeredHorizontally, animated: true)
    _ = collectionView.dequeueReusableCell(withReuseIdentifier: "DayCell", for: indexpath) as? DayCell

  }

请告诉我该如何实现它?

Please tell how can I implement it?

推荐答案

在控制器的 viewDidLoad 中,您可以编写滚动到的代码集合视图的特定索引路径。

In viewDidLoad of your controller, you can write the code for scrolling to a particular index path of collection view.

self.collectionView.scrollToItem(at:IndexPath(item: indexNumber, section: sectionNumber), at: .right, animated: false)

这篇关于如何在swift中滚动到集合视图中的特定元素索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆