简体   繁体   中英

Why pickerView doesn't text in the text field within tableView cell?

Why pickerView doesn't text in the text field within tableView cell ? When i tap on text field, then picker view appears. After hit any text, then doesn't show this text in the text field. How to resolve this issue. Help me to solve this issue.

This is the array which is used in the text field or picker view.
var arrProg = ["In Progress", "Done", "Not Done"]

//Picker View DataSource and Delegate Methods.
    func numberOfComponents(in pickerView: UIPickerView) -> Int
    {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
    {
        if pickerView == self.pickerProgress
        {
            return arrProg.count
        }
        else
        {
            return 0
        }
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
    {
        if pickerView == self.pickerProgress
        {
            return arrProg[row]
        }
        else {
            return "No Picker Selected"
        }
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {
        if pickerView == self.pickerProgress
        {
            let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryTaskCell") as! CategoryTaskTVCell
            cell.tfProgress.text = arrProg[row]

        }
    }
    //End of Picker Method



func pickerFunctionality(textField: UITextField)
    {

        let toolBar = UIToolbar()
        toolBar.barStyle = UIBarStyle.default
        toolBar.isTranslucent = true
        toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 0.9)
        toolBar.sizeToFit()

        let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(donePicker))
        doneButton.tintColor = UIColor.blue

        let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)


        let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(cancelPicker))
        cancelButton.tintColor = UIColor.blue

        toolBar.backgroundColor = UIColor.white
        toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
        toolBar.isUserInteractionEnabled = true
        textField.inputAccessoryView = toolBar

    }

    @objc private func donePicker()
    {
        self.view.endEditing(false)
    }

    @objc private func cancelPicker()
    {
        self.view.endEditing(false)
    }


Table View DataSource and Delegate methods

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryTaskCell", for: indexPath) as! CategoryTaskTVCell

        TFCatDetail.imgInTagDate(tfTag: cell.tfTag, tfDate: cell.tfDate)


        pickerProgress.dataSource = self
        pickerProgress.delegate = self
        //Picker View
        self.pickerFunctionality(textField: cell.tfProgress)
        cell.tfProgress.inputView = pickerProgress
        TextField.textFieldDesign(textField: cell.tfProgress)
}

The code let cell = tableView.dequeueReusableCell is designed to be used for the table view to return it's cell. In your case you call it in picker delegate and probably receive a new object instead of the one seen on the table view so no visual effect is seen.

There are a few approaches. Probably easiest is to retain your cell in the view controller so your cell for row would look like:

if let dateCell = self.dateCell {
    return dateCell
} else {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryTaskCell", for: indexPath) as! CategoryTaskTVCell
    ...
    self.dateCell = cell
    return cell
}

Another way is to fetch it directly as tableView.visibleCells and hope it is there. If it is not then no update is needed anyway. But in any case you need to set this text once the cell appears...

Which leads to probably the approach where you need to retain your date as apart of a data source. So instead of trying to reach the cell directly you would assign your date and reload that cell:

self.selectedString = arrProg[row]
UITableView().reloadRows(at: [self.indexPathOfCellThatWeAreusingAsInput], with: .automatic)

Now your data source method will be called again so you need to assign the new text:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryTaskCell", for: indexPath) as! CategoryTaskTVCell
    ...
    cell.tfProgress.text = self.selectedString

Although this is probably the cleanest solution I must say that this may dismiss your keyboard if you are using text field first responder. For that reason it is still probably best to just save the cell in the property...

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