为什么 pickerView 不在 tableView 单元格中的文本字段中输入文本?

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

为什么 pickerView 不在 tableView 单元格的文本字段中输入文本? 当我点击文本字段时,会出现选择器视图。点击任何文本后,文本字段中不会显示该文本。如何解决这个问题。帮我解决这个问题。

这是在文本字段或选择器视图中使用的数组。
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)
}

代码 let cell = tableView.dequeueReusableCell 旨在用于 table 视图 return 它的单元格。在您的情况下,您在选择器委托中调用它并且可能会收到一个新对象而不是在 table 视图中看到的对象,因此看不到任何视觉效果。

有几种方法。可能最简单的方法是将您的单元格保留在视图控制器中,以便您的行单元格看起来像:

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

另一种方法是直接获取它作为 tableView.visibleCells 并希望它在那里。如果不是,则无论如何都不需要更新。但是无论如何,您需要在单元格出现后设置此文本...

这可能导致您需要将日期保留为数据源的一部分。因此,与其尝试直接到达单元格,不如分配日期并重新加载该单元格:

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

现在将再次调用您的数据源方法,因此您需要分配新文本:

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

虽然这可能是最干净的解决方案,但我必须说,如果您使用的是文本字段第一响应程序,这可能会关闭您的键盘。因此,最好将单元格保存在 属性...