Segue from Uitableviewcell by Tapping on an Image Inside of Cell

Perform segue depending on image tapped in TableViews custom cell

try this instead.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell
cell.imageButton.addTarget(self, action: #selector(self.imageAction(_:)), for: .touchUpInside)
cell.imageButton.tag = indexPath.row
cell.frontImage.image = gameImages[indexPath.row]
cell.title.text = gameTitles[indexPath.row]

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "imageAction:")
cell.frontImage.isUserInteractionEnabled = true
cell.frontImage.addGestureRecognizer(tapGestureRecognizer)
return cell

}

func imageAction(_ sender:UIButton) {
switch sender.tag{
case 0:
self.performSegue(withIdentifier: "goToDonkey", sender: self)
case 1:
performSegue(withIdentifier: "goToTRex", sender: self)
case 2:
performSegue(withIdentifier: "goToSuperMarioRun", sender: self)
case 3:
performSegue(withIdentifier: "goToArcades1", sender: self)
default:
break
}
}

imageAction function is not member of self since is part o a tableview delegate function not self. that's why the unrecognized selector instance.
but i maybe rewrite the func using another delegate, but since you dnt want to use the cell didselect only the image this may solve your problem.

Making a segue from tapping on a image view in a cell

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

Segue when the `UITableViewCell is tapped in Swift

For that you need to implement didSelectRowAtIndexPath delegate method of UITableView. Please see the below code:

First of all take the global variable:

var currentIndexPath: NSIndexPath?

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

currentIndexPath = indexPath
self.performSegueWithIdentifier("showLocalPostsDetailView", sender: nil)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == localPostsDetailSegue {
if let destination = segue.destinationViewController as? LocalPostsDetailViewController {
destination.postName.text! = names[currentIndexPath.row]
}
}
}

How to get indexPath when image inside cell tapped

1: set tag of ImageView in cellForRowAtIndexpath method , tag will be equal to indexPath.row

imgView.tag = indexPath.row

2: add a target to tapGuestureRecognizer attached on imageView

3: get tag of imageView in that method

let imgView = sender as! UIImageView
let tag = imgView.tag

4: get data accordingly ,and push

let next = self.storyBoard.instatiateViewController(WithIdentifer:"detailVC") 
next.data = datas[tag]
self.navigationController.pushViewController(next)

How to bring UIImageView to foreground upon tapping a tableViewCell?

This is a Demo table view.On tapping the table view cells a view is poped up with close button. And on pressing the close button the pop up view is closed.

import UIKit

class ViewController: UITableViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell

cell.textLabel?.text = "Happy"
return cell
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("Cell\(indexPath.row) is selected")

let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let popoverVC = storyBoard.instantiateViewControllerWithIdentifier("PopViewController") as! PopViewController
popoverVC.delegate = parentViewController as? InfoViewDelegate
let nav = UINavigationController(rootViewController: popoverVC)
nav.modalPresentationStyle = UIModalPresentationStyle.Popover
nav.navigationBar.hidden = true
self.presentViewController(nav, animated: true)
{

}
popoverVC.passingViewController = self

}
}

This is a PopUpViewController:

 import UIKit
protocol InfoViewDelegate: class
{
func infoViewClicked(tag: Int)
}

class PopViewController :UIViewController
{

@IBOutlet weak var btnClose: UIButton!
var delegate = InfoViewDelegate?()
var passingViewController: UIViewController!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func viewWillAppear(animated: Bool) {
}

override func viewDidDisappear(animated: Bool) {
self.presentingViewController?.dismissViewControllerAnimated(true
, completion: {
})
}
/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
@IBAction func btnClicked(sender: UIButton) {
if(sender == self.btnClose)
{
self.delegate?.infoViewClicked(1)
self.presentingViewController?.dismissViewControllerAnimated(true
, completion: {
})
}

}

}

A table view

A simple view for Image

How to present another view controller by tapping on a label inside a tableview cell

You have to make the following steps :

  1. Define userInteractionEnabled = true for the label using Interface Builder or in code, as you wish, (VERY IMPORTANT!!) without this the tap is not enabled.
  2. Define an action to the Tap Gesture Recognizer to handle the tap inside the label.
  3. To present the another ViewController you can do one of the following two things :

    • Make a modal segue from the Tap Gesture Recognizer to the another ViewController using the Interface Builder or make an action for the Tap Gesture Recognizer and then present the another ViewController manually using the presentViewController function.

I thought the first is more easy to present the another ViewController, Is up to you.

I hope this help you.

How to make a push segue when a UITableViewCell is selected

control drag From View Controller to View Controller

From View Controller to View Controlle!

You will need to give an identifier to your segue:

Sample Image

Perform the segue:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"yourSegue" sender:self];
}

Now here is the thing, this will just perform the segue, if you ever needed to pass some data to that view controller. Then you have to implement the following segue delegate:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"yourSegue"])
{
//if you need to pass data to the next controller do it here
}
}

Swift 4 UITableViewCell first tap doesn't call segue, and second tap calls segue with the first cell data

Use this overload instead of didDeselectRowAt

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
_ = tableView.dequeueReusableCell(withIdentifier: "CardCell") as! CardTableViewCell?
self.eachCard = cardsArray[indexPath.row]

self.performSegue(withIdentifier: "showCard", sender: tableView)
}


Related Topics



Leave a reply



Submit