【问题标题】:How to scroll to a particluar index in collection view in swift如何快速滚动到collectionview中的特定索引
【发布时间】:2017-04-08 18:36:13
【问题描述】:

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

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

  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

  }

请告诉我如何实现它?

【问题讨论】:

  • 调用scrollCollectionView好像有问题。你从哪里调用这个方法?
  • 在我的收藏视图准备好之后
  • 如果您将代码发布在您创建、准备并最终添加您的集合视图以供我们查看的位置,将会很有帮助。

标签: ios iphone arrays swift uicollectionview


【解决方案1】:

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

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

【讨论】:

  • 如何将我的特定索引路径传递给 (item: indexNumber, section: sectionNumber)
  • 你的 indexPath 是什么意思?上面的代码不也是这样吗?
  • "animated: false" 对我来说是关键
【解决方案2】:

在 Swift 4 中,这对我有用:

override func viewDidLayoutSubviews() {

    collectionView.scrollToItem(at:IndexPath(item: 5, section: 0), at: .right, animated: false)
}

将它放在其他任何地方只会导致奇怪的视觉效果。

注意 - 刚刚对我的原始帖子进行了编辑,希望将 viewDidLayoutSubviews() 更改为 viewDidAppear()。我拒绝了编辑,因为当时将该函数的代码放在其他任何地方(包括viewDidAppear)都不起作用,并且该帖子已经获得了 10 次投票,这意味着它一定也适用于其他人。我在这里提到它以防有人想尝试viewDidAppear,但我很确定我会尝试过,并且不会写“将它放在其他任何地方只会导致奇怪的视觉效果”。它可能是 XCode 更新或任何解决此问题的方法;太久了,我记不住细节。

【讨论】:

  • 它在 viewDidLayoutSubview 对我有用,但每次我尝试向左或向右滑动时,它都会让我回到同一个索引
  • 不记得手头的细节,但是您的收藏视图数据是否正确填充?您是否实现了适当的委托方法?
  • 这将在每次视图控制器布局其子视图时触发,可以多次,所以我建议不要这样做。
【解决方案3】:

你可以用这个

self.collectionView.scrollToItem(at:IndexPath(item: index, section: 0), at: .right, animated: false)

【讨论】:

    【解决方案4】:

    我使用了你的答案@PGDev,但我把它放在了viewWillAppear 中,它对我很有效。

    self.collectionView?.scrollToItem(at:IndexPath(item: yourIndex, section: yourSection), at: .left, animated: false)
    

    【讨论】:

    • 我应该在“(item: yourIndex, section: yourSection)”中使用什么来聚焦特定的单元格?
    【解决方案5】:

    在你的控制器的viewDidLoad中,你可以写代码

         self.myCollectionView.performBatchUpdates(nil) { (isLoded) in
    
         if isLoded{
                     self.myCollectionView.scrollToItem(at: self.passedContentOffset, at: [.centeredVertically,.centeredHorizontally], animated: false)
                   }
          } 
    

    【讨论】:

      【解决方案6】:

      测试了这个

      func scrollToIndex(index:Int) {
           let rect = self.collectionView.layoutAttributesForItem(at:IndexPath(row: index, section: 0))?.frame
           self.collectionView.scrollRectToVisible(rect!, animated: true)
       }
      

      【讨论】:

        【解决方案7】:

        我都试过了,但对我来说,它只适用于添加额外的一行

               self.collectionView.scrollToItem(at:IndexPath(item: index, section: 0), at: .right, animated: false)
             
               collectionView.layoutSubviews()
        

        你可以在任何地方调用它。

        【讨论】:

          猜你喜欢
          • 2020-12-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-24
          • 1970-01-01
          • 2016-10-22
          相关资源
          最近更新 更多