Frank Boccia Frank Boccia - 6 months ago 8
Swift Question

UITableView Cell to a new ViewController

I am trying to go from a

UITableView Cell
, when tapping the Cell, to go to another
ViewController
in swift. I have a
viewController
called MainMenu which is my
UITableView
and I am trying to click on a cell, and go to my other
ViewController
. Can someone please help?

My code for the UITableView called Main Menu:

class MainMenu: UITableViewController {

// Set Tabs in Table View Controller
var tabs = [String]()


override func viewDidLoad() {
super.viewDidLoad()

// Name of Tabs
tabs = ["Scanner","QR-Codes","Cargo","Matrix","Xbox","PS4","Nintendo","Sega","Dreamcast","Xbox360","GameCube","Wii","Challenger","Mustang","Macbook","Logitech"]
}
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 tabs.count
}
// Set Tabs in View
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("scannerCell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel!.text = tabs[indexPath.row]
print(tabs)
// return
return cell
}
}

Answer

First you need to embed your mainmenu controller with Navigation controller in story board After that add this method in your mainmenu controller

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let detailVC = self.storyboard.instantiateViewControllerWithIdentifier("detailViewController") as! DetailViewController
    self.navigationController.pushViewController(detailVC , animated: true)
}

Hope this will help.