當您選擇任何項目進入第二個集合視圖可以顯示同一圖像到第一個集合觀點是這樣的:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if collectionView == self.collectionView2{
collectionView1.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)
}
}
下面是多收集意見完整的示例代碼:
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var collectionView1: UICollectionView!
@IBOutlet weak var collectionView2: UICollectionView!
let collectionViewAIdentifier = "CollectionViewACell"
let collectionViewBIdentifier = "CollectionViewBCell"
var imageArray = [UIImage]()
override func viewDidLoad() {
super.viewDidLoad()
imageArray = [UIImage(named: "1.jpg")!, UIImage(named: "2.jpg")!, UIImage(named: "3.jpg")!, UIImage(named: "4.jpg")!, UIImage(named: "5.jpg")!, UIImage(named: "6.jpg")!, UIImage(named: "7.jpg")!, UIImage(named: "8.jpg")!, UIImage(named: "9.jpg")!, UIImage(named: "10.jpg")!, UIImage(named: "11.jpg")!, UIImage(named: "12.jpg")!, UIImage(named: "13.jpg")!]
print(imageArray)
collectionView1.delegate = self
collectionView2.delegate = self
collectionView1.dataSource = self
collectionView2.dataSource = self
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if collectionView == self.collectionView1 {
let cellA = collectionView1.dequeueReusableCellWithReuseIdentifier(collectionViewAIdentifier, forIndexPath: indexPath) as! CollectionViewCell1
cellA.imageV.image = imageArray[indexPath.row]
return cellA
}
else {
let cellB = collectionView2.dequeueReusableCellWithReuseIdentifier(collectionViewBIdentifier, forIndexPath: indexPath) as! CollectionViewCell2
cellB.imageV.image = imageArray[indexPath.row]
return cellB
}
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if collectionView == self.collectionView2{
collectionView1.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)
}
}
}
結果:
項目Sample瞭解更多信息。
似乎很不必要的是,顯示圖像的頂視圖是一個集合視圖,難道你只是有一個'UIImageView'的視圖嗎? – Fonix