如何解决如何在不刷新键盘的情况下将FirstResponder保留在UITableView reloadRows上?
UITableView
包含一个简单的单元格列表,每个单元格包含一个UITextField
。选择textField后,它将变成firstResponder
并显示键盘。
一个附加按钮用于以编程方式更改行/ textField的内容。需要重新加载相应的单元以显示数据更改。
到目前为止没有什么特别的。但是,当在firstResponder的状态下重新加载一行时,它将退出其firstResponder状态,因此键盘消失。当然,在重新加载该单元格后,我可以在该单元格中使用becomeFirstResponder
,但这会使键盘闪烁。
如何避免这种情况?重新加载或移动单元格(或其控件)时如何保持其firstResponder状态?
示例:
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet var tableView: UITableView!
@IBOutlet var button: UIButton!
var rowTitles = ["zero","one","two","three","four","five"]
var activeRow = -1
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return rowTitles.count
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "textEditCell",for: indexPath) as? TextEditCell ?? TextEditCell()
cell.textEdit.text = rowTitles[indexPath.row]
if indexPath.row == activeRow {
cell.textEdit.becomeFirstResponder()
}
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(TextEditCell.self,forCellReuseIdentifier: "textEditCell")
}
@IBAction func click() {
// Update text of row 1,set it as active and reload the row
rowTitles[1] = rowTitles[1]+"1"
activeRow = 1
tableView.reloadRows(at: [IndexPath(row: 1,section: 0)],with: .automatic)
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。