简体   繁体   中英

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

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.

截图

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

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

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.

在此输入图像描述

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

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 .

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

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM