简体   繁体   English

点按单元格时将PickerView作为键盘输入

[英]PickerView as Keyboard Input when Cell is tapped

I'm new to iOS programming and I would appreciate your help! 我是iOS编程的新手,非常感谢您的帮助!

I have a table view cell whose right item should display a number the user picks. 我有一个表格视图单元格,其右项应显示用户选择的数字。 My current solution is a picker view as the right item but the problem is that it is too small for appropriate user experience. 我当前的解决方案是将选择器视图作为正确的项目,但问题是它对于适当的用户体验而言太小了。

Thus im thinking about a label on the right side of the cell. 因此,我正在思考单元格右侧的标签。 When the user taps the cell, a picker view should appear similar to a keyboard. 当用户点击单元格时,选择器视图应看起来类似于键盘。 The row within the picker view that the user selects should then update the label. 然后,用户在选择器视图中选择的行应更新标签。 (I'm not sure if thats a smart solution..) But I don't know how to implement this. (我不确定这是否是一个聪明的解决方案。)但是我不知道如何实现这一点。

I'm sorry if that question is too basic, but I would be very thankful for an easy explanation or solution suggestions to my user experience problem. 如果该问题太基本了,我们将感到抱歉,但是对于我的用户体验问题的简单解释或解决方案建议,我将非常感谢。

[Unfortunately I can't post a image] [很遗憾,我无法发布图片]

First create a pickview and set it as input view to your text field li so 首先创建一个pickview并将其设置为文本字段li so的输入视图

    textField.inputView = pickerView

then setup the toolbar 然后设置工具栏

func setupToolbar () {
    let toolBar: UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 44.0))
    toolBar.tintColor = Color.custom(hexString: JIFFYConstants.ApplicationConstants.kAppOrangeColor, alpha: 1.0).value
    let doneBtn: UIButton = UIButton(type: .custom)
    doneBtn.frame = CGRect(x: 10, y: 0, width: 30, height: 30)
    doneBtn.setTitle(JIFFYConstants.JiffyButtonTitles.kDoneActionTitle, for: .normal)
    doneBtn.setTitleColor(Color.custom(hexString: JIFFYConstants.ApplicationConstants.kAppOrangeColor, alpha: 1.0).value, for: .normal)
    doneBtn.addTarget(self, action: #selector(doneButtonAction), for: .touchUpInside)
    let doneBarButton: UIBarButtonItem = UIBarButtonItem(customView: doneBtn)
    let space : UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)

    var items = [UIBarButtonItem]()
    items.append(space)
    items.append(doneBarButton)
    toolBar.items = items

    textField.inputAccessoryView = toolBar
}

Later on add datasource to your picker view and reload it. 稍后将数据源添加到选择器视图并重新加载它。 It will show your pickerView as inputView. 它将您的pickerView显示为inputView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM