【发布时间】:2017-11-08 15:59:30
【问题描述】:
我有一个带有自定义单元格的 tableView,它与配置的自己的 UITableViewCell 完美配合。
在自定义单元格中有一个 UITextField 使用键盘类型的小数点。所以我想将 Done 按钮作为 inputAccessoryView 添加到键盘。
使用 doneClicked 函数和 textViewDidEndEditing 函数将委托分配给 UITextField 时出现错误。
如果我在 ViewController 中实现以下代码,我不会收到任何错误,只是我不能在重复内容(TableView)中使用 UITextField 插座。
任何想法如何得到这个工作?感谢您的帮助!
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)
}
}
固定
【问题讨论】:
-
请注意,Swift 约定以大写字母开头的类命名
-
你只需要结束 deciPadField 编辑
deciPadField.endEditing(true)和 UIBarButtonSystemItem.whatever 是多余的。只需使用.flexibleSpace
标签: ios swift uitableview custom-cell inputaccessoryview