Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在运行时更新label.text_Ios_Swift_Uitableview_Swift3_Reloaddata - Fatal编程技术网

Ios 在运行时更新label.text

Ios 在运行时更新label.text,ios,swift,uitableview,swift3,reloaddata,Ios,Swift,Uitableview,Swift3,Reloaddata,我尝试在表的一行中更新label.text值。更新应该由用户在此行的另一个文本字段中输入数字触发: 我的代码如下所示: Swift 3:ViewController import UIKit class RiskPlan: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var tableView: UITableView! var probability1 = St

我尝试在表的一行中更新label.text值。更新应该由用户在此行的另一个文本字段中输入数字触发:

我的代码如下所示:

Swift 3: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(位于:[indexPath],带有:
    UITableViewRowAnimation.top)
    不执行更新和
  • 我得到一个“致命错误:在展开可选文件时意外发现零”
    result=Int(影响1)!*Int(概率1)

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

在您的
手机中,您可以执行以下操作:

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(\uu tableView:UITableView,cellForRowAt indexath: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
  }
}


您真的只需要一行的tableview吗?此外,您还可以查看我实际使用的textfield委托方法myItem.count,因为我的表中的行数可变。我将其替换为1只是为了降低此帖子的复杂性。:-)如何实现UITextFieldTextDiEndEditing到我的代码?我现在有点迷路了。关于零值错误的问题在哪里?我将变量定义为空,这样就不会发生这样的致命错误!?谢谢工作起来很有魅力!
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)
  }

}