【发布时间】:2018-01-09 18:15:22
【问题描述】:
我正在UITableView 中创建一个表单。此表单在原型单元格中有 UILabel 和 UITextField。每个UITextField 都有一个唯一的标签。
当用户单击提交按钮时,我想使用标签从每个 UITextField 中获取价值。
现在的问题是我有 18 个UITextField,当我在字段中输入值并滚动视图以填充剩余字段时,在按下提交按钮时填充数据后,应用程序崩溃。它将无法找到第一个UITextField 的标签。
我想比较 UITextFields 的值。但我不想收集所有文本字段值。我想检索特定的文本字段值并将其与提交按钮单击时的其他文本字段值进行比较。
This is how my UITableView Looks
这是我的代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SurveyFormTableViewCell", for: indexPath) as! SurveyFormTableViewCell
var pest_code = textfield_id_value[indexPath.row].pest_code
var srno = textfield_id_value[indexPath.row].srno
var inspection_no = textfield_id_value[indexPath.row].inspection_no
cell.text_field.layer.cornerRadius = 3
cell.text_field.layer.borderWidth = 1
cell.text_field.addTarget(self, action: #selector(tableFieldDidChange(_textField:)), for: .editingChanged)
let id:Int = Int("\(srno)\(pest_code)\(inspection_no)")!
cell.text_field.tag = id
cell.label?.text = struc_array[indexPath.item].pest
cell.label?.numberOfLines = 0
cell.label?.lineBreakMode = NSLineBreakMode.byWordWrapping
cell.layer.borderWidth = 1
return cell
}
@IBAction func submit_survey_clicked(_ sender: UIButton) {
text_field = self.table_view.viewWithTag(textfield_tag1) as! UITextField
text_field2 = self.table_view.viewWithTag(textfield_tag2) as! UITextField
}
【问题讨论】:
-
This answer 类似的问题可能有用。
-
我不想收集所有文本字段值。我想检索特定的文本字段值并将其与其他文本字段值进行比较。假设我想检索第四行文本字段值并将其与第三行文本字段值进行比较,那么我将如何实现呢?
-
关键是你必须收集所有文本字段的值,然后才能将它们滚动出屏幕。否则单元格将被重复使用,标签将被更改,您将无法恢复 textField 值。
标签: ios swift uitableview uitextfield