xcode UICollectionView 滚动到索引处的项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31148121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:24:10  来源:igfitidea点击:

UICollectionView scroll to item at Index

xcodeswiftuicollectionview

提问by Mohamed Horani

I've succeeded to scroll item at index e.g. if the user taps a button it's goes to the last item

我已经成功地在索引处滚动项目,例如,如果用户点击一个按钮,它会转到最后一个项目

Here is the code that i've used

这是我使用过的代码

 self.collection.scrollToItemAtIndexPath(NSIndexPath(forItem: self.array.count - 1, inSection: 0), atScrollPosition: UICollectionViewScrollPosition.Top, animated: true) 

But my issue is how to know if a user is at the bottom or at the top of the UICollectionView?

但我的问题是如何知道用户是在底部还是顶部UICollectionView

I can do this by creating 2 functions and if a user taps on the TOP UIButtonit goes up and vice versa...

我可以通过创建 2 个函数来做到这一点,如果用户点击顶部,UIButton它会上升,反之亦然......

But I only want to do this with a single UIButton. so that if the user presses on the button the code should know if a user is at the bottom and goes up and vice versa. I looked at each function at UICollectionViewbut doesn't seem to returnat which item the user at.

但我只想用一个UIButton. 这样如果用户按下按钮,代码应该知道用户是否在底部并上升,反之亦然。我查看了每个功能,UICollectionView但似乎没有查看return用户所在的项目。

I thought of something which logically might be correct as this one

我想到了一些逻辑上可能正确的东西作为这个

var someBool: Bool!

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
 let tempIndex: Int = self.array.count / 2 

 if indexPath.row > tempIndex{
   someBool = true // UP
 } else {
  someBool = false // Down
 }
 ...
}

@IBAction func buttonPressed{
   if someBool{
     self.collection.scrollToItemAtIndexPath(NSIndexPath(forItem: 0, inSection: 0), atScrollPosition: UICollectionViewScrollPosition.Top, animated: true) 
   } else {
     self.collection.scrollToItemAtIndexPath(NSIndexPath(forItem: self.array.count - 1, inSection: 0), atScrollPosition: UICollectionViewScrollPosition.Top, animated: true) 
   }
}

This Mean let's assume an array has 9 elements / 2 = 4 (INT) so if the user selects item 9, 9 > 4 true go up else down.

这意味着让我们假设一个数组有 9 个元素 / 2 = 4 (INT) 所以如果用户选择项目 9, 9 > 4 true 向上否则向下。

I believe this code works well only if the user selects an item, but I need an alternative way without using the indexPath.rowbecause, let's assume the user didn't select any item... so this code won't work....

我相信只有当用户选择一个项目时,这段代码才能很好地工作,但我需要一种不使用的替代方法,indexPath.row因为,让我们假设用户没有选择任何项目......所以这段代码将不起作用......

回答by hardikdevios

if you are supporting from iOS 8.0

如果您支持iOS 8.0

optional func collectionView(_ collectionView: UICollectionView,
             willDisplayCell cell: UICollectionViewCell,
          forItemAtIndexPath indexPath: NSIndexPath)

This new API is use full so your code will be perfectly worked just move your code from didSelectItemAtIndexPathto this method because it calls every time and check your indexpath.rowhere. let me know if it helps.

这个新的 API 是完全使用的,所以你的代码将完美地工作,只需将你的代码从didSelectItemAtIndexPath这个方法移动到这个方法,因为它每次都会调用并检查你的indexpath.row这里。如果有帮助,请告诉我。