简体   繁体   中英

how to display a new tableview from user selecting row in tableview swift

I am trying to figure how to segue to a new view when the user selects a certain row in the current tableview. I am thinking this will be done by the didselectrowatindexpath method but can not figure out the logic of how it will work. I would like it to display a view with the specific courses in the subject the user selected. I think this will use an array? I would appreciate any help here thanks. 在此处输入图片说明

Yes, you're right, you can do this be responding to didSelectRowAtIndexPath.

You can have a view controller as your table view delegate that implements these methods:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    if let thing = self.things.objectAtIndex(indexPath.row) as? Thing
    {
        self.performSegueWithIdentifier("OpenThing", sender: thing)
    }
}

Where "OpenThing" is a segue defined in my storyboard.

Then I have overridden the prepareForSegue method:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    if segue.identifier == "OpenThing"
    {
        if let controller = segue.destinationViewController as? ThingViewController
        {
            if let thing = sender as? Thing
            {
                controller.thing = thing
            }
        }
    }
}

This is a contrived example of having an array of "Things". I access a thing by row index then segue to the next view controller, providing the thing that I want to display.

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