简体   繁体   English

如何在tableView中选择单元格时触发选择器视图

[英]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. UIPickerViewUIView子类,因此您可以像任何其他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.

相关问题 tableView单元格内的PickerView - PickerView inside tableView cell 手动触发单元格选择TableView - Manually trigger cell select TableView 自定义tableview单元格中的UITextField。 点击单元格/文本字段时显示pickerview - UITextField in custom tableview cell. Show pickerview when cell/textfield is tapped 当表格视图中的单元格为表格视图iPhone时选择行 - Select row when cell in tableview is a tableview iPhone 为什么pickerView在tableView单元格的文本字段中没有文本? - Why pickerView doesn't text in the text field within tableView cell? 当向左滑动表格视图单元格到手机的左边缘时,它将自动触发删除UITableViewRowAction,如何禁用此功能? - when left swipe a tableview cell to the left edge of the phone, it will auto trigger the delete UITableViewRowAction , how to disable this? 同时选择另一个表格单元格时如何取消选择表格单元格? - How to deselect tableview cell when selecting another tableview cell in the meantime? 如何在不重用的情况下选择表视图中的第一个单元格? - How to select the first cell in a tableview without reusing it? 如何在swift4中选择取消选择tableview单元格 - How to select deselect tableview cell in swift4 点按单元格时将PickerView作为键盘输入 - PickerView as Keyboard Input when Cell is tapped
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM