Greg Peckory
Greg Peckory

Reputation: 8068

Open new View Controller by clicking Cell in Table View - Swift iOS

I have the following onClick function

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

    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    let row = indexPath.row
    println("Row: \(row)")

    println(meetingArray[row] as! String)

}

which prints out the text on the cell which is clicked. It is working fine.

I'm just wondering how you would set a function which would direct you to the new view controller

Upvotes: 41

Views: 86664

Answers (5)

Saurabh Prajapati
Saurabh Prajapati

Reputation: 2380

In storyboard, set storyboardId of your viewController in Identity Inspector.

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

    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    let row = indexPath.row
    println("Row: \(row)")

    println(meetingArray[row] as! String)

    let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("storyBoardIdFor your new ViewController") as SecondViewController
    self.navigationController.pushViewController(secondViewController, animated: true)
}

Upvotes: 9

Jaywant Khedkar
Jaywant Khedkar

Reputation: 6141

Try This it's working for me,

   func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("You tapped cell number \(indexPath.section).")
    print("Cell cliked value is \(indexPath.row)")

    if(indexPath.row == 0)
    {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    let controller = storyboard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController

    self.navigationController?.pushViewController(controller, animated: true)

    }
  }

Hope this will help for some one .

Upvotes: 1

Suragch
Suragch

Reputation: 512696

For other users coming to this question, if you have a static cells in a UITableViewController, you can just control drag from the cell to the new View Controller.

enter image description here

You can also do this on dynamic cells if you don't need to pass any data to the next View Controller. However, if you do need to pass data then you should make the segue from the Table View's view controller itself and not the cell. Then call the segue programmatically.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.performSegue(withIdentifier: "mySegueIdentifier", sender: self)
}

Upvotes: 4

Eendje
Eendje

Reputation: 8883

Programmatically:

let destination = UIViewController() // Your destination
navigationController?.pushViewController(destination, animated: true)

Storyboard:
First you'll have to set an identifier for your view. In the screenshot below you can see where to enter the identifier.

screenshot

After that, you can create a "destination" and push it to the navigation controller by using the code below:

let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let destination = storyboard.instantiateViewController(withIdentifier: "YourViewController") as! YourViewController
navigationController?.pushViewController(destination, animated: true)

Segue:
First you'll have to set an identifier for your segue as shown below:

segue1

segue2

performSegue(withIdentifier: "segue", sender: self)

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "segue" {
        // Setup new view controller
    }
}

EDIT: Updated for Swift 3.x

Upvotes: 66

L.N.Vu
L.N.Vu

Reputation: 379

if using,

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

The value of selected row pass to another view controller too late. You'd better to pass the selected row in the func prepareForSegue as below, at first, declare a global variable to pass value to other viewController

var globalVar:Int 

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.
        let selectedIndex = self.tableView.indexPathForCell(sender as! UITableViewCell)
        globalVar = selectedIndex!.row
        print("tableView prepareForSegue: " + String(globalVar))

    }

Upvotes: -1

Related Questions