为什么pickerView不在tableView单元格的文本字段中发短信?

问题描述 投票:0回答:1

为什么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)
}
ios swift uitableview uipickerview
1个回答
0
投票

代码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

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

© www.soinside.com 2019 - 2024. All rights reserved.