Sharon
Sharon

Reputation: 11

Click ImageView which is inside a CollectionViewCell to go to ViewController

I have an ImageView inside a CollectionViewCell. I want to be able to click the image and it take me to another ViewController. How would I do this? This is the code I have so far.

import UIKit

class FirstViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
    @IBOutlet weak var collectionView: UICollectionView!

    var images = ["meal1", "photograph1", "meal1"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! CollectionViewCell

        //set images
        cell.imageView.image = UIImage(named: images[indexPath.row])

        return cell
    }
}

Upvotes: 0

Views: 2469

Answers (4)

Jerry Chong
Jerry Chong

Reputation: 9240

Swift 5

CollectionView Function:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell

    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.tap(_:)))

    cell.yourImg.isUserInteractionEnabled = true
    cell.yourImg.tag = indexPath.row
    cell.yourImg.addGestureRecognizer(tapGestureRecognizer)

    return cell
}

Tap Function:

@IBAction func tap(_ sender:AnyObject){
    print("ViewController tap() Clicked Item: \(sender.view.tag)")
}

Upvotes: 1

Zohaib Hassan
Zohaib Hassan

Reputation: 984

Add a tabGestureRecognizer to your imageview in collectionView "cellForItemAt" method, and in the method of recognizer tap call the segue to go to the desired viewcontroller.

Upvotes: 1

Maulik Pandya
Maulik Pandya

Reputation: 2220

As you said you want to detect image tap on collectionview cell please go through this code :

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.connected(_:)))

    cell.yourImageView.isUserInteractionEnabled = true
    cell.yourImageView.tag = indexPath.row
    cell.yourImageView.addGestureRecognizer(tapGestureRecognizer)

And add below method to your ViewController

func connected(_ sender:AnyObject){
     print("you tap image number : \(sender.view.tag)")
     //Your code for navigate to another viewcontroller
    }

Note - Make sure your user interection for cell image is enable

Upvotes: 2

Benhamine
Benhamine

Reputation: 153

You can either us a UIButton and set the image property on it with no title, or you can add a UIGestureRecognizer to the UIImageView. Either way, you'd just Present or Show the ViewController you want to display once the action has been received.

One thing I'll often do in this situation, is create a CollectionCellDelegate protocol that has a callback function (something like buttonPressed:forCollectionCell:), that I can have my CollectionView conform to, then set the delegate of each cell to the CollectionView. Then you can call up to the CollectionView when the button/image is pressed, and have the CollectionView handle whatever behaviour you want, in this case, presenting/pushing a new view controller.

Upvotes: 0

Related Questions