我UITableView
约20行均含有一UITextField
。第一次点击一个文本框会打开键盘,我准备编辑这个文本框。如果我点击下一个文本框(注意键盘始终显示),键盘仍然显示,但蓝色光标不在新文本框中,我无法输入任何文本。但是如果我再次点击另一个文本框,它就可以正常工作。这种行为交替发生,一次它工作而另一次不工作。的UITextField中的UITableView不成为第一个响应者
委托方法textFieldShouldBeginEditing(_:)
总是被调用,阉我可以编辑或没有。代理方法textFieldDidBeginEditing(_:)
仅在编辑工作时调用。
这是cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldCell")!
let titleLabel = cell.viewWithTag(1) as! UILabel
let contentTextField = cell.viewWithTag(2) as! FixableTextField
contentTextField.delegate = self
contentTextField.inputAccessoryView = doneToolbar
// Enable/disable editing for text fields
if isEditing {
contentTextField.enableEditing()
} else {
contentTextField.disableEditing()
}
// Present Profile Data
if profileUpdateBuffer != nil {
switch indexPath.row {
case 0:
titleLabel.text = "Count"
contentTextField.text = "\(profileUpdateBuffer!.count)"
contentTextField.purposeID = "count"
contentTextField.keyboardType = .numberPad
case 1:
titleLabel.text = "City"
contentTextField.text = "\(profileUpdateBuffer!.city)"
contentTextField.purposeID = "city"
contentTextField.keyboardType = .default
// ...
case 20:
titleLabel.text = "Name"
contentTextField.text = "\(profileUpdateBuffer!.name)"
contentTextField.purposeID = "name"
contentTextField.keyboardType = .default
default:
titleLabel.text = ""
contentTextField.text = ""
}
return cell
}
// No data available -> show info in first row
else {
if indexPath.row == 0 {
titleLabel.text = "No data"
contentTextField.text = "No data"
}
else {
titleLabel.text = ""
contentTextField.text = ""
}
return cell
}
}
的enableEditing()
和disableEditing()
方法的代码是从FixableTextField
类。我可以看到文本框始终处于启用状态,因为我可以看到文本框边框
// Extract from FixableTextField class
func enableEditing() {
self.isEnabled = true
self.borderStyle = .roundedRect
}
func disableEditing() {
self.isEnabled = false
self.borderStyle = .none
}
代码从setProperty
方法,它是从ProfileData
类UITextField
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
// Delete empty field indicator "-"
if textField.text == "-" {
textField.text = ""
}
//Move profileTable's contentView to correct position
if textField is FixableTextField {
let path = IndexPath(row: rowMap[(textField as! FixableTextField).purposeID]!, section: 0)
moveContentViewUp(indexPath: path)
}
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
// Save new value to profileUpdateBuffer
do {
try self.profileUpdateBuffer?.setProperty(value: textField.text!, key: (textField as! FixableTextField).purposeID)
} catch ProfileError.PropertySettingWrongType {
let falseInputAlert = UIAlertController(title: "False Input", message: "The input for this field is not valid.", preferredStyle: .alert)
falseInputAlert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(falseInputAlert, animated: true, completion: nil)
} catch {
print("Error when trying to set property for profileUpdateBuffer in ProfileViewController")
}
// Display new data in table
profileTable.reloadData()
}
提取物。 profileUpdateBuffer
的类型是ProfileData
func setProperty(value:String, key:String) throws {
switch key {
case "count":
count = value
case "city":
count = value
// ...
case "name":
name = value
default:
throw ProfileError.PropertySettingWrongType
}
}
请发布您的代码,以便我们可以帮助您。尤其是你的cellForRowAt indexPath函数。你如何添加监听器到你的textFields? –
谢谢,当然我会的。 – Codey
'textFieldShouldBeginEditing(_ :)'中是否有过滤逻辑?我看到你也有你的VC设置为所有文本字段的代表。你有一个逻辑来区分委托方法实现中的单元格吗?您可以尝试在单元格中移动TextField委托,这样每个单元格都将负责自己。 – kr45ko