gpt4 book ai didi

ios - UITableView 中的 UITextField 没有成为第一响应者

转载 作者:行者123 更新时间:2023-11-28 15:31:19 25 4
gpt4 key购买 nike

我有大约 20 行的 UITableView,每行包含一个 UITextField。我第一次点击文本字段将打开键盘,我已准备好编辑此文本字段。如果我点击下一个文本字段(注意键盘一直显示),键盘仍然显示,但蓝色光标不在新文本字段中,我无法输入任何文本。但是,如果我再次点击另一个文本字段,它就可以正常工作。此行为交替出现,一次有效,另一次无效。

委托(delegate)方法 textFieldShouldBeginEditing(_:) 总是被调用,无论我是否可以编辑。委托(delegate)方法 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
}

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()
}

ProfileData 类的 setProperty 方法中提取。 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
}
}

最佳答案

我制作了一个小程序来模仿您描述的行为。看来问题是由 textFieldDidEndEditing(_:) 结束时重新加载 TableView 数据引起的:

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()
}

尝试删除 profileTable.reloadData() 以进行实验以确认问题的根本原因(是的,您的其他单元格将不会更新)。

解决此问题的一种方法是在 textFieldDidEndEditing(_:) 中利用 visibleCells 上的直接单元格更新。我看到 profileUpdateBuffer? 是您的数据模型。如果您的单元格的 titleLabel 和 textField 属性位于 TableView 的可见单元格属性中,则只需从您的模型中手动更新它们。

如果您想相应地调整单元格的大小,请使用 AutoLayout 和 UITableViewAutomaticDimension 结合 beginUpdates()/endUpdates() 获取表格 View 的行高> 通话。

有关如何在不失去键盘焦点的情况下实现直接单元格操作和/或动态单元格大小更新的更多详细信息 check the accepted answer on this question我已经回答过了。

希望这会有所帮助!

关于ios - UITableView 中的 UITextField 没有成为第一响应者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44707827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com