2017-07-28 83 views
0

我有一个集合视图,当我点击一个按钮时,它应该移动到下一个集合视图单元格。正如你在下面的图片中看到的,我有下一个按钮,而不是手动刷新它将会转到enter image description here自己的下一个索引。我尝试了谷歌搜索,但没有找到任何答案。Collection View Segue In Code Swift

+1

的[UICollectionView scrollToItemAtIndexPath(https://stackoverflow.com/questions/19816184/uicollectionview-scrolltoitematindexpath) – the4kman

+0

可能的复制这是一件好事,但这么老的语法是不正确的。你能给我同样的答案,但正确的语法? – josh

回答

0

你想要的不是一个segue,segue主要在视图控制器之间切换。就像@ the4kman提到的,这已经在Objective-C中得到了解答,但我可以将它转换成Swift 3。

你想要做的是右键单击并从下一个按钮拖入类,并将其作为一个动作。确保它在touchUpInside上。这将创建一个IBAction函数,该函数将在按下按钮时调用。

@IBAction func nextBtn_pressed(_ sender: Any) { 

    let index = 1 //this variable has to be the index of the next cell 
    collectionView.scrollToItem(at: IndexPath(item: index, section: 0), at: .left, animated: true) 
    //you can change .left to whatever style you want 

} 
+0

这个工程,但如果一个想让它再继续一次,我会怎么做呢? – josh

+0

像你想要它一次移动2页? – Timmy