Search code examples
iosswiftuitableviewuitextfielduipickerview

UITextField in custom tableview cell. Show pickerview when cell/textfield is tapped


I have a UITextField in a custom tableview cell. when the user presses either the cell (or the UITextField), a pickerview should show instead of the keypad....i'm using the textfields "inputview" and "accesoryview" properties to accomplish this.

okay. So when i select the cell, the pickerview shows up with its picker values....however when i select the textfield itself (which is within the cell), the picker view shows up but the picker values don't show up...i only see one empty cell in the picker view.

what approach should I use so that whether the user presses either the textfield or cell, the same exact thing happens (that is, the pickerview shows up with picker values). Off the top is it obvious to you what could be causing this problem?

UITextfield in TableViewCell. Show UIPickerView

I have removed all the clutter from my code and kept just the part pertaining to the question (i'm hoping i haven't omitted relevant code):

import UIKit

class EditProfileViewController: UIViewController, UITableViewDelegate,UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate,UITextViewDelegate {
    let properties = ["Name","Birthday","Diet"]
    let diets:NSArray = ["Vegan","Plant-based","Vegetarian","Meat Eater"]
    var userEditProfileTable : UITableView!
    var reminderCells = (1...3).map{ _ in EditProfileTableViewCell()}
    var toolBar : UIToolbar!
    var i:Int!
    var myUIPicker = UIPickerView()
    var editorViewController: EditorViewController!
    var myValues: NSArray!



override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = UIColor.blue

    let barHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height
    let displayWidth: CGFloat = self.view.frame.width
    let displayHeight: CGFloat = self.view.frame.height
    userEditProfileTable = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight - barHeight))

    userEditProfileTable.delegate = self
    userEditProfileTable.dataSource = self
    userEditProfileTable.rowHeight = 40

    userEditProfileTable.register(EditProfileTableViewCell.self, forCellReuseIdentifier: "cellId")

    self.userEditProfileTable.reloadData()
    self.view.addSubview(userEditProfileTable)

}


func generalPicker(){
    myUIPicker.becomeFirstResponder()
    self.myUIPicker.delegate = self
    self.myUIPicker.dataSource = self
}

@objc func donePicker() {
    reminderCells[i].valueTextField.resignFirstResponder()

}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    reminderCells[indexPath.item].valueTextField.becomeFirstResponder()
    i = indexPath.item
    print(properties[indexPath.item])
    switch properties[indexPath.item] {
    case "Name":
        print("jkehdfgjdhj")
    case "Birthday":
        print("jhkjhjh")
    case "Diet":
        myValues = diets
        generalPicker()
        print(properties[indexPath.item])
    default:
        print("Some other character")
    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // retuen no of rows in sections
    return properties.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // retuen your custom cells
    print(indexPath)
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! EditProfileTableViewCell
    cell.backgroundColor = UIColor.clear
    cell.property.text = properties[indexPath.item]
    cell.isUserInteractionEnabled = true
    cell.valueTextField.isUserInteractionEnabled = true
    cell.valueTextField.delegate = self

    toolBar = UIToolbar()
    toolBar.sizeToFit()

    switch properties[indexPath.item]{
        case "Birthday":
            print("bonita")
        case "Diet":
            let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(donePicker))
            toolBar.setItems([doneButton], animated: false)
            cell.valueTextField.inputAccessoryView = toolBar
            cell.valueTextField.inputView = myUIPicker
        default:
            print("mamacita")
    }

    reminderCells[indexPath.item] = cell
    return cell
}

// data method to return the number of column shown in the picker.
func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

// data method to return the number of row shown in the picker.
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

    print(myValues.count)
    return myValues.count
}

// delegate method to return the value shown in the picker
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    print("mjkhjkhkj")
    print(myValues[row])
    return myValues[row] as? String
}

// delegate method called when the row was selected.
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    print("row: \(row)")
    print("value: \(myValues[row])")

    reminderCells[i].valueTextField.text = myValues[row] as? String
}

}


Solution

  • You set data source for picker in didSelect , you need to add it to textfield delegate this line myValues = diets

    // UITextField Delegates
        func textFieldDidBeginEditing(_ textField: UITextField) {
             myValues = diets  // be addedd
             generalPicker()
        }