2017-07-28 60 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