gpt4 book ai didi

ios - 在运行时更新 label.text

转载 作者:搜寻专家 更新时间:2023-11-01 06:37:25 24 4
gpt4 key购买 nike

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

enter image description here

我的代码是这样的:

Swift 3: View Controller

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(位于:[indexPath],其中:
    UITableViewRowAnimation.top)
    不做更新和
  • 我收到一个“ fatal error :在展开一个 Optional 时意外发现 nilresult = Int(impact1)! * Int(probability1)!
  • 的值"

最佳答案

如果您想知道何时在文本字段中完成更改,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)
}

}

关于ios - 在运行时更新 label.text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39854831/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com