在运行时更新 label.text
Update label.text at runtime
我尝试让 label.text 值在 table 的一行中得到更新。更新应该由用户在此行的另一个文本字段中输入数字触发:
我的代码如下所示:
Swift3:ViewController
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)!
的价值”
如果我没记错的话,您想根据在文本字段中插入的内容更新标签的文本。
在您的 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)"
}
}
}
希望对您有所帮助。
如果您想知道文本字段中的更改何时完成,func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
不是您要放置计算代码的地方。
相反,您应该在 CellCustomized
class.
的文本字段上收听事件 .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)
}
}
我尝试让 label.text 值在 table 的一行中得到更新。更新应该由用户在此行的另一个文本字段中输入数字触发:
我的代码如下所示:
Swift3:ViewController
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)!
的价值”
如果我没记错的话,您想根据在文本字段中插入的内容更新标签的文本。
在您的 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)"
}
}
}
希望对您有所帮助。
如果您想知道文本字段中的更改何时完成,func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
不是您要放置计算代码的地方。
相反,您应该在 CellCustomized
class.
.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)
}
}