验证 uitableview 单元格中多个 uitext字段的下一个按钮上的非空



在我的视图控制器中,我有一个表视图,我在其中加载多个表视图单元格。有些具有UITextfield,标签,单选按钮和复选按钮。现在我已经完成了在tableview单元格中显示和输入数据的所有部分,但无法检查任何一个字段是否留空。 在我的控制器中单击按钮时,我需要检查此验证是否所有字段都不为空。或者如何在单击按钮时从每个字段中获取数据。 这是我的cellForRowAt 索引路径代码,它将给出我正在加载的差异单元格的想法。如何验证我的 uitext字段是否为空并选中单选按钮。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.row {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "StartEndDateCell", for: indexPath) as! StartEndDateCell
//cell with 2 textfield
return cell
case 1:
let cell = tableView.dequeueReusableCell(withIdentifier: "NameTableViewCell", for: indexPath) as! NameTableViewCell
//cell with single textField
return cell
case 2:
let cell = tableView.dequeueReusableCell(withIdentifier: "StartEnddateTableViewCell", for: indexPath) as! StartEnddateTableViewCell
//cell open UIDATEPICKER here on click of textfield contains 2 textfield
return cell
case 3:
let cell = tableView.dequeueReusableCell(withIdentifier: "AmountTableViewCell", for: indexPath) as! AmountTableViewCell
//cell with single textfield with numeric keyboard
return cell
case 4:
let cell = tableView.dequeueReusableCell(withIdentifier: "MaxFixTableViewCell", for: indexPath) as! MaxFixTableViewCell
return cell
//cell with radio buttons
case 5:
let cell = tableView.dequeueReusableCell(withIdentifier: "infoTableViewCell", for: indexPath) as! infoTableViewCell
//for oneTimeLabel
let tapGestureFrequency : UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(freqLblClick(tapGestureFrequency:)))
tapGestureFrequency.delegate = self as? UIGestureRecognizerDelegate
tapGestureFrequency.numberOfTapsRequired = 1
cell.oneTimeLabel.isUserInteractionEnabled = true
cell.oneTimeLabel.tag = 1
cell.oneTimeLabel.addGestureRecognizer(tapGestureFrequency)

//for onLabel
let tapGestureOnDebit : UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(lblClickDebit(tapGestureOnDebit:)))
tapGestureOnDebit.delegate = self as? UIGestureRecognizerDelegate
tapGestureOnDebit.numberOfTapsRequired = 1
cell.onLabel.isUserInteractionEnabled = true
cell.onLabel.tag = 2
cell.onLabel.addGestureRecognizer(tapGestureOnDebit)
return cell
case 6:
let cell = tableView.dequeueReusableCell(withIdentifier: "RemarksTableViewCell", for: indexPath) as! RemarksTableViewCell
return cell
case 7:
let cell = tableView.dequeueReusableCell(withIdentifier: "InformTableViewCell", for: indexPath) as! InformTableViewCell
return cell

default:
let cell = tableView.dequeueReusableCell(withIdentifier: "checkTableViewCell", for: indexPath) as! checkTableViewCell
return cell
}
}

我认为您可以实现UITextFieldDelegate来跟踪和更新数据,以便使用UITextField进行UITableViewCell。但这需要您在需要更新数据集的每个单元格中编写实现文本委托。此外,对于开关控件和复选框,您可以监视其valueChanged事件。

例如

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let text = textField.text, let textRange = Range(range, in: text) {
let updatedText = text.replacingCharacters(in: textRange, with: string)
UserData.shared.name = updatedText
}
return true
}
@IBAction func switchValueChanged(_ sender: Any) {
UserData.shared.setting = switchControl.isOn
}

您需要将此数据存储在某个位置,特别是因为 UITableView 重用其单元格,因此如果用户滚动,您可能会丢失输入的内容。

假设您有一个数组,每个单元格的信息来自该数组,您可以创建一个标志,如果用户回答了该问题,则可以显式标记。在cellForRowAtindexPath,您将检查该字段是否为空并重新加载答案/状态。

之后,您可以创建一个 validationMethod,该验证方法应在每次单元格输入后根据您的本地字段数组 + 答案调用。

我在单元格类中完成的所有其他验证,例如数字类型和日期类型。我知道这不是正确的方法。我们可以将我们的值保存到模型类并使用模型类而不是这样做。当我们滚动表视图时,以这种方式执行此操作可能会导致验证失败,但现在它正在我的代码中工作。我仍然在等待以书面方式进行。我不会将我的问题标记为已回答

单击按钮时


@IBAction func nextButtonClicked(_ sender: Any) {
let nameCell = self.tableView.cellForRow(at: IndexPath(row: 1, section: 0)) as? NameTableViewCell
if (nameCell?.nameTextField.text!.isEmpty)! {
Log.i("TextField is Empty---" )
}
let startEndDateCell = self.tableView.cellForRow(at: IndexPath(row : 2,section : 0)) as? StartEnddateTableViewCell
if((startEndDateCell?.startDateTextField.text!.isEmpty)! || (startEndDateCell?.endDateTextField.text!.isEmpty)!){
Log.i("Start And End Date Empty")
}
let amountCell = self.tableView.cellForRow(at: IndexPath(row : 3, section : 0))as? AmountTableViewCell
if (amountCell?.amountTextField.text!.isEmpty)! {
Log.i("Amount Cell Empty---")
}
else{
let vc = NextViewController(nibName: "NextViewController", bundle: nil)
navigationController?.pushViewController(vc, animated: true )
}
}

最新更新