我正在尝试将变量添加到TextView子类中,但是一旦执行此操作,我就无法使textViewDidChange正常工作,我认为这是因为委托。
它没有子类就可以工作,并打印itemOrderID。如何将其与自定义子类一起使用?
class textViewSub: UITextView {
var itemOrderID: Double = 0.0
}
class TextviewCell: UITableViewCell, UITextViewDelegate {
@IBOutlet weak var textView: textViewSub!
@IBOutlet weak var textViewLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
textView.delegate = self
}
func textViewDidChange(_ textView: textViewSub) {
let id = textView.tag
let text = textView.text
let itemOrderID = textView.itemOrderID
print(itemOrderID)
}
}
cellForRowAt代码:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TextviewCell", for: indexPath) as! TextviewCell
cell.tag = indexPath.row
cell.textView.tag = indexPath.row
cell.textView.itemOrderID = checklistData[indexPath.section].checklistItems?[indexPath.row].itemOrderID ?? 0.0
cell.textView.delegate = cell
return cell
}
您的textViewDidChange
功能与UITextViewDelegate
协议不匹配。应该是:
func textViewDidChange(_ textView: UITextView) {
guard let sub = textView as? textViewSub else { return }
let id = sub.tag
let text = sub.text
let itemOrderID = sub.itemOrderID
print(itemOrderID)
}