0

I created a tableview in storyboard that contains 11 rows. I want to be able to tap on each row and have it open up into it's own tableview controller, where I will store data, depending on the row tapped.

      var array = ["row 1", "row 2", "row 3", "row 4", "row 5", "row 6", "row 7", "row 8", "row 9", "row 10", "row 11"]

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return array.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("prototypeCellIdentifier", forIndexPath: indexPath)
        cell.textLabel?.text = array[indexPath.item]
        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator 
    return cell
}

This is what I have done to create the rows.

icestorm0806
  • 691
  • 1
  • 9
  • 20
  • 2
    UITableViewDelegate method `func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)` – Asdrubal Aug 05 '16 at 17:55
  • Learn to read Apple documentation. This is all in there. Or search the internet. There are countless tutorials and examples in the internet about exactly this. – dasdom Aug 05 '16 at 18:23
  • @dasdom I think you may be onto something. If only everyone did this and Stack overflow did not have to exist. – icestorm0806 Aug 05 '16 at 19:33
  • @icestorm0806 http://stackoverflow.com/help/how-to-ask – dasdom Aug 05 '16 at 19:57

1 Answers1

1

In short:

  1. Go to your Storyboard
  2. Add another TableViewController to the Storyboard
  3. Press and hold the right mouse button on the tableViewCell of the TableView with 11 rows and drag it towards the newly created TableViewController
  4. Release the mouse and a popup will appear.
  5. Choose one of the items e.g. show
  6. Build and run your app, click on a cell and a new TableViewController will open

To send data to the ViewController that's been shown by clicking a row, you should look into the method prepareForSegue(_)

Bacon
  • 792
  • 5
  • 8
  • There is also a lot of good answers here on SO on how to pass data between view controllers. Maybe you could link one of them? – Losiowaty Aug 05 '16 at 21:36
  • 1
    @Losiowaty, good point :) more details about sending data when using segues: http://stackoverflow.com/a/26089243/5489170 and I've also found a more detailed explanation about how to show a new ViewController when clicking on an UITableViewCell: http://stackoverflow.com/q/28323925/5489170 – Bacon Aug 05 '16 at 21:43