2015-10-12 59 views
1

我從圖像選取器中獲取多個圖像,將它保存到兩個集合視圖中並製作一個像這樣的視圖。如何鏈接相互連接的兩個集合視圖?

enter image description here

正如你看到的,有兩個收集時,我拍了拍在第二集合視圖的第二圖像鏈接至各other.Like,第一個集合視圖顯示第二集合的抽頭圖像view.So,如何鏈接兩個相互連接的收藏視圖。這是我從畫廊或相機中選取多個圖像後的詳細圖像選擇視圖。

這可能嗎?

如果有人能幫我一把,我會很開心。 :)

待辦事項下一步:當我得到這個,我將在第二個收集視圖的每個圖像添加刪除按鈕。並添加圖像「添加按鈕」後面的「img_4」,使用戶添加更多的圖像到這兩個uicollection視圖。

+1

似乎很不必要的是,顯示圖像的頂視圖是一個集合視圖,難道你只是有一個'UIImageView'的視圖嗎? – Fonix

回答

4

當您選擇任何項目進入第二個集合視圖可以顯示同一圖像到第一個集合觀點是這樣的:

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) 
     } 
    } 

} 

結果:

enter image description here

項目Sample瞭解更多信息。

+0

謝謝你,先生。我真的很高興你支持我。我在這裏呆了兩週,我仍然找不到任何答案。謝謝。如果你繼續支持我的下一步,我將很高興。我更新了我的但是,我會先自己嘗試。如果我有任何問題,我會在這裏發佈另一個問題。謝謝。:) –

+0

高興地幫助你.. :) –