简体   繁体   中英

PickerView as Keyboard Input when Cell is tapped

I'm new to iOS programming and I would appreciate your help!

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

    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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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