【问题标题】:How to get TextField Input and why the input repeat when I scroll?如何获取 TextField 输入以及为什么滚动时输入重复?
【发布时间】:2017-01-31 07:37:18
【问题描述】:

我这样创建 TableViewCells:

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ErnaehrungTableViewCell", for: indexPath) as! ErnaehrungTableViewCell
    let row = indexPath.row
    cell.LabelType.text = cellValues[indexPath.row]
    cell.TextValue.keyboardType = .decimalPad
    cell.TextValue.tag = indexPath.row

    cell.selectionStyle = .none
    cell.ButtonUnit.addTarget(self, action: #selector(ErnaehrungManualController.ButtonUnitClick), for: .touchUpInside)
    cell.ButtonUnit.tag = indexPath.row`

LabelType 是一个标签,TextValue 是一个文本字段,而 ButtonUnit 是一个按钮。 它看起来像这样:

如果我在前五个文本字段(如图所示)输入并滚动,则输入也在其他行中。

我该如何解决这个问题?

如果我按下右上角的按钮 (Fertig),那么我想阅读所有 TextField 的文本。我有这个代码,但我不工作:

for var i in 0...cellValues.count-1 {
        let indexPath = IndexPath(row: i, section: 0)
        let cell = tableView.dequeueReusableCell(withIdentifier: "ErnaehrungTableViewCell", for: indexPath) as! ErnaehrungTableViewCell

        if let text = cell.TextValue.text, !text.isEmpty
        {
            let textString = cell.TextValue.text!
            print(textString)

        }
    }

那么我该如何解决 TextFields(我没有输入文本)不复制其他 TextFields 的输入?

以及如何读出所有行的 TextField.text?

感谢您的帮助,如果您需要更多代码,我会添加。

【问题讨论】:

  • 您已经编写了复杂的逻辑,这里实际的问题不在于您的代码,它是 dequeueReusableCell 将如何工作的基本功能,它只是复制旧单元格以形成新单元格。请先阅读相关内容,并尝试为少量输入编写代码,这将真正帮助您解决问题。
  • 我该如何解决?或者我怎样才能做得更好?
  • 我只是去看看你的代码,很快就会回来提供一些建议..
  • 我认为,我以前使用过,使用数组来存储您在文本字段中写入的任何内容,并使用该数组从表中获取值,使用 for 循环和单元格获取值,使用数组将输入存储到文本字段并与输出相同。
  • 我应该将数据写入数组并将其从 TextField 中删除吗?但是我在 TextField 上看不到输入,对吗?

标签: ios swift xcode textfield


【解决方案1】:

这是因为重复使用单元格的单元格出列。为了避免这个问题,将值存储在一个数组中,然后在调用表视图委托时设置该值。在 SWIFT 3 中

var dataList = ["","","",""] // Array size should be same as the total row count


func textFieldShouldEndEditing(_ textField: UITextField) -> Bool { 

    if textField.text != ""{

        dataList[textField.tag] = textField.text!
    }
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "ErnaehrungTableViewCell", for: indexPath) as! ErnaehrungTableViewCell
    let row = indexPath.row
    cell.LabelType.text = cellValues[indexPath.row]
    cell.TextValue.text = dataList[indexPath.row] // Setting the value here
    cell.TextValue.keyboardType = .decimalPad
    cell.TextValue.tag = indexPath.row
    cell.TextValue.delegate = self // Setting delegate here
    cell.selectionStyle = .none
    cell.ButtonUnit.addTarget(self, action: #selector(ErnaehrungManualController.ButtonUnitClick), for: .touchUpInside)
    cell.ButtonUnit.tag = indexPath.row`

}

【讨论】:

  • 我试试,给我一点时间
  • 欢迎好友 :-)
猜你喜欢
  • 1970-01-01
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多