Search code examples
iosswiftuitableviewuipickerview

How to trigger a pickerview when select a cell in a tableView


I have a tableView with 7 cells like this: enter image description here

I wanna trigger some events when you select a cell. For example, start editing the username when you tap the Username row. And pop up a picker view at the bottom with Male/Female selection inside when you tap the Gender row. As far as I know, I need to put those events inside this:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

}

But I have no idea how to accomplish this. Anyone has ideas? Thank you in advance.


Solution

  • You are correct. Putting the logic in didSelectRowAtIndexPath is a good way to go.

    How you do it is to write code. There is no stock answer.

    If you want content to appear on top of the current window then you will need to handle that yourself. On iPad, you could use a popover, but popovers are not supported natively on iPhone/iPod touch. You might look at using a 3rd party popover library that offers popover support for iPhone. There are several on Github, and probably several on Cocoa Controls as well. I've used one before, but it had a few issues, so I wouldn't recommend it.

    If you are ok presenting a whole new view controller then simply define a new view controller in your storyboard, give it a unique identifier, use instantiateViewControllerWithIdentifier to create it, then presentViewController:animated: to display it modally.