2016-10-04 99 views
1

我嘗試使label.text值在表格的一行中更新。更新應該由該行中另一個文本框輸入數字用戶觸發:在運行時更新label.text

enter image description here

我的代碼如下所示:

斯威夫特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)!
+0

你真的需要的只是1行中的tableview?此外,你可以看看textfield委託方法[這裏](https://developer.apple.com/reference/uikit/uitextfielddelegate) –

+0

我實際上使用myItem.count,因爲我的表中有可變數量的行。我用1替換它只是爲了減少這篇文章的複雜性。 :-)如何實現UITextFieldTextDidEndEditing到我的代碼?我現在有點迷路了。關於無值錯誤的問題在哪裏?我將變量定義爲空,所以不會發生像這樣的致命錯誤!? – Jake2Finn

回答

3

如果你想知道什麼時候在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) 
    } 

} 
+0

謝謝!奇蹟般有效! – Jake2Finn

+0

沒問題,很高興幫助你! – Wilson

0

如果我沒有錯,你想根據你在文本框中插入的內容來更新標籤的文本。

在你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)" 
     } 
    } 
} 

希望幫助。