1

In my Xcode project I want to create a segue from tapping on an image in a cell in a tableview. I have search around for an answer and the most relevant one was to change the image view to a button. This solution unfortunately does not work with my project because I have the image view customized in a certain way I want for when a image is in it.

So now I am in desperate need of help. I have a button with text in another cell that performs the same segue I want. It gets the objectID that I put in that cell and appends it to a global variable so when the other view appears, it will take that objectID and do the necessary functions. I have copied the code for that button below:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell") as! Cell1
    cell.textButton.layer.setValue(indexPath, forKey: "index")
    return cell
}

@IBAction func textPressed(_ sender: Any) {
    let index = (sender as AnyObject).layer.value(forKey: "index") as! IndexPath

    let cell = tableView.cellForRow(at: index) as! Cell1

    globalText.append(cell.objectID)
    let text = self.storyboard?.instantiateViewController(withIdentifier: "TextVC") as! TextVC
    self.navigationController?.pushViewController(text, animated: true)
}

Now I just need to know how to do this exact same thing but for a imageview in a cell. Can someone please help?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
fphelp
  • 1,544
  • 1
  • 15
  • 34
  • "have a button with text in another cell" is there any relation with the cell with image? If no, just statically create cell and use the button click – Saranjith Aug 09 '17 at 06:15

1 Answers1

1

Create a segue from your view controller to your destination and give it an "identifier" then

In cell for row

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(viewImage(_:)))
imageView.isUserInteractionEnabled = true
imageView.tag = tag!
imageView.addGestureRecognizer(gesture)

Create function to perform segue

func viewImage(_ sender: AnyObject) {
    let tag = sender.view.tag
    // do what ever with tag
    performSegue(withIdentifier: "identifier", sender: self)
}
Amorn Narula
  • 323
  • 3
  • 13