sekaisan
sekaisan

Reputation: 441

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.

Upvotes: 5

Views: 6673

Answers (3)

Jam
Jam

Reputation: 288

Basically, you can make each cell has its own picker view.

open class DatePickerTableViewCell: UITableViewCell {

  let picker = UIDatePicker()

  open override func awakeFromNib() {
    super.awakeFromNib()
    picker.datePickerMode = UIDatePickerMode.date
  }

  open override var canBecomeFirstResponder: Bool {
    return true
  }

  open override var canResignFirstResponder: Bool {
    return true
  }

  open override var inputView: UIView? {
    return picker
  }
  ...
}

And then in your didSelectRowAt, just make the cell becomeFirstResponder:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
 if let cell = tableView.cellForRow(at: indexPath) as? DatePickerTableViewCell {
  if !cell.isFirstResponder {
    _ = cell.becomeFirstResponder()
  }
 }
}

You can check my library for detail: https://github.com/hijamoya/PickerViewCell

Upvotes: 3

Duncan C
Duncan C

Reputation: 131418

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.

Upvotes: 1

Adnan Aftab
Adnan Aftab

Reputation: 14477

UIPickerView is subclass of UIView, so you can add and use it same like any other UIView object. for your specific recquirment you should create an object of UIPickerView and show and hide it when necessary. So create a UIPickerView and add above the table view inside view in which you added tableView and in didSelectRowAtIndexPath set pickerView.hidden = false

And also you can animate it from bottom via

UIView.animateWithDuration(1, animations: { () -> Void in
            // And set final frame here
        })

Upvotes: 0

Related Questions