Ios 验证uitableview单元格中多个uitextfields的“下一步非空”按钮
在我的视图控制器中,我有一个表视图,在其中加载多个表视图单元格。有些有UITextfield、标签、单选和复选按钮。现在我已经完成了在tableview单元格中显示和输入数据的所有部分,但无法检查是否有任何字段留空。 在我的控制器中单击按钮时,如果所有字段均为非空,我需要检查此验证。或者,我如何在单击按钮时从每个字段获取数据。 下面是我的cellForRowAt indexpath代码,它将给出我正在加载的diff cell的概念。如何验证我的uitextfield是否为空且单选按钮是否已选中Ios 验证uitableview单元格中多个uitextfields的“下一步非空”按钮,ios,swift,xcode,validation,uitextfield,Ios,Swift,Xcode,Validation,Uitextfield,在我的视图控制器中,我有一个表视图,在其中加载多个表视图单元格。有些有UITextfield、标签、单选和复选按钮。现在我已经完成了在tableview单元格中显示和输入数据的所有部分,但无法检查是否有任何字段留空。 在我的控制器中单击按钮时,如果所有字段均为非空,我需要检查此验证。或者,我如何在单击按钮时从每个字段获取数据。 下面是我的cellForRowAt indexpath代码,它将给出我正在加载的diff cell的概念。如何验证我的uitextfield是否为空且单选按钮是否已选中
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
}
}
我认为您可以使用
UITextField
实现UITableViewCell
s来跟踪和更新数据。但这需要在需要更新数据集的每个单元格中编写ImplementText委托。此外,对于开关控件和复选框,您可以监视它们的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重用了其单元格,因此如果用户滚动,您可能会丢失输入的内容 假设您有一个数组,其中每个单元格的信息来自该数组,您可以创建一个标志,如果用户回答了该问题,则该标志将明确显示。在
cellforrowatinexpath
上,您将检查字段是否为空并重新加载答案/状态
然后,您可以创建一个validationMethod,该方法应在每次单元格输入后根据您的本地字段+答案数组调用。我在单元格类中完成的所有其他验证,如数字类型和日期类型。我知道这不是正确的方法。我们可以将我们的值保存到模型类中,并使用模型类而不是这样做。当我们滚动tableview时,这样做可能会导致验证失败,但目前它在我的代码中起作用。我仍然在等待着以书面的方式来完成它。我不会把我的问题标记为已回答 按一下按钮
@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 )
}
}
事实上,这是我的错误,我在问题中没有提到我需要在哪里实现验证,我需要在它导航到其他视图控制器的按钮上检查验证。若任何文本字段留空,按钮保持不选中,我需要显示错误或弹出窗口。基本上,我需要避免用户导航到其他视图控制器,直到所有文件都被填充。如何在单击按钮时获取文件中输入的数据。您的按钮是否属于UITableViewCell的一部分?可能不会,在这种情况下,您只需要更新答案中提到的数据,并且需要在实际导航之前验证该数据。bhai:谢谢您更新答案,它确实为我提供了一种方法。请用空验证更新它,就像我在回答中所做的那样。如果有效,我会将您的答案标记为已接受。我为每个单元格设置了不同的TableViewCell类,在这些单元格中,我完成了选择和输入数据的工作,并显示这些数据,因为每个单元格都具有不同的功能。在cellForRowAtIndexPath上,我可以检查字段是否为空,但需要在完成导航的按钮上设置验证。我现在需要了解如何访问tableview/tableview单元格,以便在单击按钮时检查其是否为空。