我无法理解。怎么办,试过这个:
class myViewCell: UITableViewCell, UITextFieldDelegate {
@IBOutlet weak var myEdit: UITextField!
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// don`t work ((
}
}
class myTableViewControler: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "NamePhoneCell", for: indexPath) as! myViewCell
…
returns cell
}
}
我有一个想法 - Delegete。但是如何,我需要委托(delegate) -shouldChangeCharactersIn 方法,该方法是 UITableViewCell 本身从 UITextFieldDelegate 委托(delegate)的
最佳答案
您需要为您创建的 UITextField 设置委托(delegate)。 myViewCell 将实现 UITextField 的委托(delegate)功能。
class myViewCell: UITableViewCell, UITextFieldDelegate {
@IBOutlet weak var myEdit: UITextField!
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// don`t work ((
}
}
override func awakeFromNib() {
super.awakeFromNib()
myEdit.delegate = self
}
关于ios - 如何处理 TextField 的 shouldChangeCharactersIn(如果它位于 TabelViewControler 的自定义单元格中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49034742/