I have some tableview cells loaded like this..
Here, the qty
field gets value from the picker field. But the issue is though both fields does show the picker fields, the data as per what is selected in the picker field is shown in the 2nd field only and in the 1st field, though I select a value from the picker field, that value is shown in the 2nd picker field instead of the 1st.
My picker view has 10 values defined in viewDidLoad
like so...
self.noOfItems = ["1","2","3","4","5","6","7","8","9","10"]
The picker view has been defined in my tableview cellForRowAt
like so...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: sellTableViewCell = tableView.dequeueReusableCell(withIdentifier: "sellProductIdentifier") as! sellTableViewCell
cell.delegate = self
let pickerView = UIPickerView()
pickerView.delegate = self
cell.qtyPickerField.inputView = pickerView
cell.qtyPickerField.text = noOfItems[0]
if let cell = cell.qtyPickerField.superview?.superview as? sellTableViewCell {
myCell = cell
}
return cell
}
My picker view methods have been given like so...
//Picker view methods
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return noOfItems.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return noOfItems[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
myCell.qtyPickerField.text = noOfItems[row]
}
I'm giving you hint.
Because in you code you added
myCell = cell
means your last cell is added in the myCell so here your second cell is last cell and if you wish to edit value by picker then you can change only last cell textfield's value not specific selected cell.Here you need add specific cell in myCell instead of last one. You can do it by use of
delegate
method ofUITextField
get specific cell byNow you can use
myCell.qtyPickerField....
add the below lines into your
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
then add the below lines into your class
then use the below lines of codes
hope this will solve your issue :)