[英]How can I access textfield of customcell in extension class?
我必須在我的擴展中到達文本字段以獲取更新文本,但我無法調用擴展。 這個文本字段是我的自定義單元格的一部分,我在表格視圖中使用它。 我必須在擴展名中到達這個文本字段並更改文本。 我怎樣才能得到這個?
我的 tableview 中的這段代碼我想更改我的擴展中的 dateTextField 文本以用於選擇器視圖。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 2 {
let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldCell", for: indexPath) as! TextFieldCell
let datePicker = UIPickerView()
datePicker.delegate = self
cell.dateTextField.inputView = datePicker
datePicker.backgroundColor = .white
cell.dateTextField.text = secilenTarih
return cell
}
這是我的選擇器視圖擴展:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
secilenTarih = tarihArray[row]
// In here I want to access dateTextField.
}
您需要將標簽添加到選擇器,然后您可以從標簽訪問您的單元格,然后做任何您想做的事情。
datePicker.tag = indexpath.row
現在在選擇器委托中,您可以像這樣獲得單元格
let path = IndexPath.init(row: pickerView.tag, section: 2)
let cell = tableView.cellForRow(at: path)
現在您可以像這樣更改文本字段中的值
cell.dateTextField.text = "your value"
我認為你可以用不同的方式做到這一點。 但我相信巧妙的方法是創建一個自定義選擇器並為您的文本字段提供參考。
這可以做類似的事情
class CustomPicker: UIPickerView {
var pickerHost: UITextField?
required init(textField: UITextField?) {
super.init(frame: CGRect.zero)
showsSelectionIndicator = true
backgroundColor = UIColor.white
self.pickerHost = textField
delegate = self
dataSource = self
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func dismiss() {
endEditing(true)
}
}
extension CustomPicker: UIPickerViewDelegate {
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pickerHost?.text = "set your text here"
}
}
希望這對你有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.