我嘗試使label.text值在表格的一行中更新。更新應該由該行中另一個文本框輸入數字用戶觸發:在運行時更新label.text
我的代碼如下所示:
斯威夫特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)
不做更新和- 我得到一個「致命的錯誤:意外發現零而展開的可選 價值」爲
result = Int(impact1)! * Int(probability1)!
你真的需要的只是1行中的tableview?此外,你可以看看textfield委託方法[這裏](https://developer.apple.com/reference/uikit/uitextfielddelegate) –
我實際上使用myItem.count,因爲我的表中有可變數量的行。我用1替換它只是爲了減少這篇文章的複雜性。 :-)如何實現UITextFieldTextDidEndEditing到我的代碼?我現在有點迷路了。關於無值錯誤的問題在哪裏?我將變量定義爲空,所以不會發生像這樣的致命錯誤!? – Jake2Finn