ios - 如何处理 TextField 的 shouldChangeCharactersIn(如果它位于 TabelViewControler 的自定义单元格中)

标签 ios swift uitableview uitextfield

我无法理解。怎么办,试过这个:

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/

相关文章:

ios - 键盘通知

swift - UISceneDelegate 和 UIApplicationDelegate 的 "didBecomeActive"和 "willResignActive"方法对之间的区别?

ios - 有时尝试从 UITableView 中删除行时会崩溃

iphone - 分组单元格显示在我的普通 tableView 中,我不知道为什么

ios - 如何获取 Moment 相册以及我的 collectionView 中的所有个人相册?

ios - 在第一个 View Controller 上的 TableView 中显示数组中的数据

ios - 我应该在我的 iOS 应用程序中加密 Amazon S3 key 吗?

ios - 更新自动布局约束以重新定位文本字段

ios - 表格单元格行中的 objective-c 不同的uilabel

ios - 在 iOS 中使用选项卡更改容器 View 内容