如何使用 Swift 在 CustomCell 中将 inputAccessoryView 实现到 UITextField?

How to implement inputAccessoryView to UITextField in CustomCell using Swift?

我有一个带有自定义单元格的 tableView,它与自己配置的 UITableViewCell 完美配合。

在自定义单元格中有一个 UITextField,它使用键盘类型的 Decimal Pad。所以我想将完成按钮作为 inputAccessoryView 添加到键盘。

使用 doneClicked 函数和 textViewDidEndEditing 函数将委托分配给 UITextField 时出现错误。

如果我在 ViewController 中实现以下代码,我不会收到任何错误,只是我无法在重复内容 (TableView) 中使用 UITextField outlet。

关于如何完成这项工作有什么想法吗?感谢您的帮助!

class favCell: UITableViewCell, UITextFieldDelegate  {

    @IBOutlet weak var deciPadField: UITextField!

    override func awakeFromNib() {
        super.awakeFromNib()

        self.deciPadField.delegate = self

        // Decipad config for adding Done button above
        let toolBar = UIToolbar()
        toolBar.sizeToFit()
        let flexiableSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
        let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(self.doneClicked))
        toolBar.setItems([flexiableSpace, doneButton], animated: false)
        deciPadField.inputAccessoryView = toolBar  
    }


    @objc func doneClicked(){
        self.view.endEditing(true)
    }

    func textViewDidEndEditing(_ textView: UITextView) {
        self.view.endEditing(true)
    }
}

固定

您正在使用 UITextViewDelegate 而不是 UITextFieldDelegate。 然后我建议为您的单元格设置协议并管理 ViewController.

中的文本字段委托操作