mox*_*mlb 1 xcode textfield ios swift
我像这样创建 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`
Run Code Online (Sandbox Code Playgroud)
LabelType 是一个标签,TextValue 是一个文本字段,ButtonUnit 是一个按钮。它看起来像这样:

如果我在前五个 TextField 中输入(如图所示)并滚动,则输入也位于其他行中。
我该如何解决?
如果我按下右上角的按钮 (Fertig),那么我想阅读所有 TextFields 的文本。我有这个代码,但我不工作:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
那么如何解决 TextFields(我没有输入文本的地方)不复制其他 TextFields 的输入的问题?
以及如何读出所有行的 TextField.text ?
感谢您的帮助,如果您需要更多代码,我会添加。
这是因为重用单元的单元出列。为了避免这个问题,将该值存储在一个数组中,然后在调用表视图委托时设置该值。在 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`
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1582 次 |
| 最近记录: |