icestorm0806
icestorm0806

Reputation: 711

How to open up a new tableview programmatically in swift

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.

Upvotes: 0

Views: 833

Answers (1)

Bacon
Bacon

Reputation: 812

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

Upvotes: 1

Related Questions