在运行时更新 label.text [英] Update label.text at runtime

查看:50
本文介绍了在运行时更新 label.text的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在表格的一行中更新 label.text 值.更新应该由用户在此行的另一个文本字段中输入数字触发:

I try to make a label.text value get updated in a row of a table. The update is supposed to be triggered by a user entering a number in another textfield in this row:

我的代码如下所示:

Swift 3:视图控制器

import UIKit

class RiskPlan: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!
    var probability1 = String()
    var impact1 = String()
    var riskFactor = String()

    var result = Int()

func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.tableView!.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CellCustomized

    impact1 = (cell.impact?.text)!
    probability1 = (cell.probability?.text)!

    result = Int(impact1)! * Int(probability1)!
    cell.riskFactor?.text = String(result)

    self.tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.top)

        return cell
    }
}

Swift 3:CellCustomized

import UIKit

class CellCustomized: UITableViewCell {

    @IBOutlet weak var probability: UITextField!
    @IBOutlet weak var impact: UITextField!
    @IBOutlet weak var riskFactor: UILabel!    

}

我的问题是

  • self.tableView.reloadRows(at: [indexPath], with:UITableViewRowAnimation.top) 不做更新和
  • 我收到一个致命错误:在解开一个 Optional 时意外发现 nilvalue" for result = Int(impact1)! * Int(probability1)!
  • self.tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.top) does not do the update and
  • I get a "fatal error: unexpectedly found nil while unwrapping an Optional value" for result = Int(impact1)! * Int(probability1)!

推荐答案

如果您想知道 textFields 中的更改何时完成,func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell 不是你想放计算代码的地方.

If you want to know when changes are done in the textFields, func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell is not the place you want to put your calculation code.

相反,您应该在 CellCustomized 类的文本字段上监听 .editingChanged 事件.

Instead you should listen to the event .editingChanged on text fields from your CellCustomized class.

class RiskPlan: UIViewController, UITableViewDelegate, UITableViewDataSource {

  @IBOutlet weak var tableView: UITableView!

  func numberOfSections(in tableView: UITableView) -> Int {
    return 1
  }

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = self.tableView!.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CellCustomized

    return cell
  }
}

<小时>

class CellCustomized: UITableViewCell {

  @IBOutlet weak var probability: UITextField!
  @IBOutlet weak var impact: UITextField!
  @IBOutlet weak var riskFactor: UILabel!

  var probability1 = 0
  var impact1 = 0

  override func awakeFromNib() {
    super.awakeFromNib()

    probability.addTarget(self, action: #selector(textOnTextFieldDidChange(textField:)), for: .editingChanged)
    impact.addTarget(self, action: #selector(textOnTextFieldDidChange(textField:)), for: .editingChanged)
  }

  func textOnTextFieldDidChange(textField: UITextField) {
    if textField === probability {
      probability1 = Int(textField.text!) ?? 0
    } else if textField === impact {
      impact1 = Int(textField.text!) ?? 0
    }

    riskFactor.text = String(probability1 * impact1)
  }

}

这篇关于在运行时更新 label.text的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆