Thiha Aung
Thiha Aung

Reputation: 5106

How to link two collection view that are connected to each other?

I am getting multiple images from image picker save it into two collection view and make a view like this.

enter image description here

As you see,there was two collection which link to each other.Like when I tapped the second image at second collection view,the first collection view show the tapped image of second collection view.So,how to i link two collection view that are connected to each other.That was detail image selection view after I picked muliple images from gallery or camera.

Is this possible?

I will be very happy if somebody can give me a hand. :)

TODO NEXT : When I got that,I will going to add delete button at each image at second collection view.And add image "Add Button" after "img_4" for making user add more images into these two uicollection view.

Upvotes: 1

Views: 1095

Answers (1)

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71852

When you select any item into second collection view you can show same image into first collection view this way:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    if collectionView == self.collectionView2{
        collectionView1.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)
    }
}

Below is complete example code for multiple collection views:

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

}

Result:

enter image description here

Project Sample for more Info.

Upvotes: 4

Related Questions