【问题标题】:Update label.text at runtime在运行时更新 label.text
【发布时间】:2016-10-04 14:24:04
【问题描述】:

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

我的代码如下所示:

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 时意外发现 nil result = Int(impact1)! * Int(probability1)! 的值”

【问题讨论】:

  • 你真的需要一个只有 1 行的表格视图吗?此外,您可以查看文本字段委托方法 here
  • 我实际上使用了 myItem.count,因为我的表中有可变数量的行。我将其替换为 1 只是为了降低这篇文章的复杂性。 :-) 如何在我的代码中实现 UITextFieldTextDidEndEditing?我现在有点失落。关于零值错误的问题在哪里?我将变量定义为空,所以不会发生这样的致命错误!?

标签: ios swift uitableview swift3 reloaddata


【解决方案1】:

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

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

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)
  }

}

【讨论】:

  • 谢谢!像魅力一样工作!
【解决方案2】:

如果我没记错的话,您想根据您在文本字段中插入的内容更新标签的文本。

在您的CellCustomized 中,您可以这样做:

class CellCustomized: UITableViewCell {

    // assuming that the outlets has been renamed...
    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var label: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()

        textField.addTarget(self, action: #selector(editing(sender:)), for: .editingChanged)
    }

    func editing(sender: UITextField) {
        // checking if the input is convertible to a number, and then add 5 for it (you can do your own operations)
        if let string = sender.text, Int(sender.text!) != nil {
            let int = Int(string)

            label.text = "\(int! + 5)"
        }
    }
}

希望有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-08
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多