【发布时间】: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