问题
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]
}
回答1:
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 of UITextField
get specific cell by
if let cell = myTextField.superview.superview as? sellTableViewCell {
myCell = cell
}
Now you can use myCell.qtyPickerField....
回答2:
add the below lines into your func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
cell.qtyTxtFld.tag = indexPath.row
cell.qtyTxtFld.addTarget(self, action: #selector(self.PickerViewClicked(_:)), for: UIControlEvents.editingDidBegin)
then add the below lines into your class
func PickerViewClicked(_ sender : UITextField)
{
myindexPath = IndexPath(row: sender.tag, section: 0)
let cell = self.theTableView.cellForRow(at: myindexPath) as! sellTableViewCell
cell.qtyTxtFld.inputView = self.thePickerView
}
then use the below lines of codes
func numberOfComponentsInPickerView(_ pickerView: UIPickerView) -> Int
{
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
return pickerArray.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
return pickerArray[row] as? String
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
let cell = self.theTableView.cellForRow(at: myindexPath) as! sellTableViewCell
cell.qtyTxtFld.text = pickerArray[row] as? String
cell.qtyTxtFld.resignFirstResponder()
}
hope this will solve your issue :)
来源:https://stackoverflow.com/questions/47323404/different-picker-values-in-different-cells-of-same-tableview