简体   繁体   中英

Dynamic TableView Cell to Multiple Detail Views Swift

I'm using presentViewController to navigate between two views in XCode. I was wondering how I might select a cell row from a dynamic tableView to navigate to a view controller. So, basically, when I tap a dynamic cell, each cell navigates to its own different view. How might I do this? So far I have this code:

@IBAction func press(sender: AnyObject) {

    let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

    let vc: UINavigationController = storyboard.instantiateViewControllerWithIdentifier("newViewController") as! UINavigationController

    self.presentViewController(vc, animated: true, completion: nil)

}

But, instead of using a button, I would like each cell to lead to another particular view as I said above.

thanks in advance

Create an array that holds the view controller identifiers:

let identifiers = ["newViewController", "newViewController2", "newViewController3"]

Implement this UITableViewDelegate method in your view controller:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if let identifier = identifiers[indexPath.row] {
        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc: UINavigationController = storyboard.instantiateViewControllerWithIdentifier("newViewController") as! UINavigationController
        self.presentViewController(vc, animated: true, completion: nil)
    }
}

Don't forget to set your tableView's delegate to the view controller that contains the tableview and that implements the above delegate method.

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