繁体   English   中英

为什么pickerView在tableView单元格的文本字段中没有文本?

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

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

这是在文本字段或选择器视图中使用的数组。
var arrProg = [“进行中”,“完成”,“未完成”]

//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旨在用于表视图以返回其单元格。 在您的情况下,您可以在选择器委托中调用它,并且可能会收到一个新对象,而不是在表视图中看到的对象,因此看不到任何视觉效果。

有几种方法。 可能最简单的方法是将单元格保留在视图控制器中,以便行单元格如下所示:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM