代码之家  ›  专栏  ›  技术社区  ›  Jake2Finn

更新标签。运行时文本

  •  1
  • Jake2Finn  · 技术社区  · 9 年前

    我试着做一个标签。在表的一行中更新文本值。更新应该由用户在该行的另一个文本字段中输入数字触发:

    enter image description here

    我的代码如下所示:

    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)
    • 我得到一个“致命错误:在打开可选 价值 result = Int(impact1)! * Int(probability1)!
    2 回复  |  直到 9 年前
        1
  •  3
  •   Wilson    9 年前

    如果您想知道文本字段中的更改何时完成, func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 不是要放置计算代码的位置。

    相反,你应该倾听事件 .editingChanged 关于 CellCustomized

    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)
      }
    
    }
    
        2
  •  0
  •   Ahmad F    9 年前

    如果我没有弄错的话,您希望根据您在文本字段中插入的内容来更新标签的文本。

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

    希望这有帮助。

    推荐文章