[英]How to trigger a pickerview when select a cell in a tableView
I have a tableView with 7 cells like this: 我有一个带有7个单元格的tableView,如下所示:
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.
先感谢您。
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
: 然后在你的
didSelectRowAt
,只需将单元格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 您可以查看我的库以获取详细信息: https : //github.com/hijamoya/PickerViewCell
You are correct. 你是对的。 Putting the logic in didSelectRowAtIndexPath is a good way to go.
将逻辑放在didSelectRowAtIndexPath中是一个很好的方法。
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.
在iPad上,您可以使用弹出窗口,但iPhone / iPod touch上不支持弹出窗口。 You might look at using a 3rd party popover library that offers popover support for iPhone.
您可能会考虑使用第三方popover库来为iPhone提供popover支持。 There are several on Github, and probably several on Cocoa Controls as well.
在Github上有几个,也可能有几个在Cocoa Controls上。 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. 如果你可以展示一个全新的视图控制器,那么只需在故事板中定义一个新的视图控制器,给它一个唯一的标识符,使用
instantiateViewControllerWithIdentifier
创建它,然后presentViewController:animated:
以模态方式显示它。
UIPickerView
is subclass of UIView
, so you can add and use it same like any other UIView
object. UIPickerView
是UIView
子类,因此您可以像任何其他UIView
对象一样添加和使用它。 for your specific recquirment you should create an object of UIPickerView
and show and hide it when necessary. 对于您的特定要求,您应该创建
UIPickerView
的对象,并在必要时显示和隐藏它。 So create a UIPickerView
and add above the table view inside view in which you added tableView and in didSelectRowAtIndexPath
set pickerView.hidden = false
因此,创建一个
UIPickerView
并在视图中添加上面的视图,在其中添加了tableView,并在didSelectRowAtIndexPath
设置了pickerView.hidden = false
And also you can animate it from bottom via 而且你也可以从底部通过它制作动画
UIView.animateWithDuration(1, animations: { () -> Void in
// And set final frame here
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.