如何让 Eureka 在我的自定义行上触发 onChange() 事件?

How to let Eureka trigger onChange() event on my custom Row?

我写了一个自定义行,行的值是这样的:

struct CountUnit:Equatable,InputTypeInitiable{

    var totalCount:Int
    var unit:String

    init?(string stringValue: String) {
        //...
    }

    static func ==(lhs: CountUnit, rhs: CountUnit) -> Bool {
        //...
    }
}

我想问的是:如何让 Eureka 在任何 CountUnit 的实例值发生变化时调用 onChange() 事件???(totalCount 和 unit):

<<< CountUnitRow() {row in
    //do some init...
}.onChange {row in
    //how to let user know value was changed
}

CountUnitRow 代码是这样的,但它几乎是空的:

final class CountUnitRow: Row<CountUnitCell>,RowType{
    required init(tag: String?) {
        super.init(tag: tag)

        cellProvider = CellProvider<CountUnitCell>(nibName: "CountUnitCell")
    }
}

CountUnitCell 在这里,我在 CountUnitCell UI 中有一个 UITextField,我想利用 Eureka 的 UITextField 自动键盘功能,所以 CountUnitCell 继承自 _FieldCell:

class CountUnitCell:_FieldCell<CountUnit>,CellType{
  @IBOutlet weak var countLbl:UILabel!
  @IBOutlet weak var stepper:UISlider!

  var count:Int = 1{
      didSet{
          countLbl.text = "\(count)"
          row.value?.totalCount = count
      }
  }

  @IBAction func countChanged(_ sender:UISlider){
      count = Int(sender.value)
  }

  func updateUI(){
      if let value = row.value{
          stepper.value = Float(value.totalCount)
          count = value.totalCount
          textField.text = value.unit
      }
  }


  override func textFieldDidEndEditing(_ textField: UITextField) {
      //super.textFieldDidEndEditing(textField)

      guard let text = textField.text,text.count > 0 else {return}

      row.value?.unit = text
  }

  override open func update() {
      updateUI()
  }

  override open func setup() {
      super.setup()

      height = {return UITableViewAutomaticDimension}

      textField.delegate = self

      updateUI()
  }

  override open func didSelect() {
      super.didSelect()
      row.deselect()
  }

  required public init(style: UITableViewCellStyle, reuseIdentifier: String?) {
      super.init(style: style, reuseIdentifier: reuseIdentifier)
  }

  required public init?(coder aDecoder: NSCoder) {
      super.init(coder: aDecoder)
  }
}

我想出来了!

答案超级简单:Eureka make hook on your Value type,全自动!!!

谢谢尤里卡!!!