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

时间:2018-02-19 12:16:50

标签: ios swift eureka-forms

我写了一个自定义的Row,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)
  }
}

1 个答案:

答案 0 :(得分:1)

我明白了!

答案非常简单:Eureka勾选你的Value类型,它是全自动的!

谢谢尤里卡!