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
UICollectionView scroll to item at Index
提问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 UIButton
it 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 UICollectionView
but doesn't seem to return
at 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.row
because, 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 didSelectItemAtIndexPath
to this method because it calls every time and check your indexpath.row
here. let me know if it helps.
这个新的 API 是完全使用的,所以你的代码将完美地工作,只需将你的代码从didSelectItemAtIndexPath
这个方法移动到这个方法,因为它每次都会调用并检查你的indexpath.row
这里。如果有帮助,请告诉我。