TextView 自定义子类 - textViewDidChange 不触发

TextView custom subclass - textViewDidChange does not trigger

我正在尝试向我的 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)
  }