简体   繁体   中英

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.

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

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