在我的视图控制器中,我有一个表视图,在其中加载了多个表视图单元格。有些具有UITextfield,Labels,Radio和复选按钮。现在,我已经完成了在表视图单元格中显示和输入数据的所有部分,但是无法检查是否有任何字段为空。
在控制器上单击按钮时,如果所有字段都不为空,则需要检查此验证。或者单击按钮时如何从每个字段中获取数据。
这是我的cellForRowAt索引路径代码,它将给出我正在加载的差异单元格的想法。如何验证我的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
}
}
最佳答案
我认为您可以使用UITextFieldDelegate
实施UITableViewCell
来跟踪和更新您的数据。但这要求您在需要更新数据集的每个单元中编写实现文本委托。此外,对于开关控件和复选框,您可以监视其UITextField
事件。
例如
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
}
关于ios - 验证uitableview单元格中多个uitextfield的下一个按钮上的非空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57972538/