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

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

问题描述

我们有一个集合视图.我制作了一个水平显示日期的日历&我水平滚动日期.现在在屏幕上我只看到 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

  }

请告诉我如何实现它?

推荐答案

在你的控制器的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)

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

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