[英]How to validate each row textfield in tableview in swift
在这里,我得到每一行文本字段的文本,其minLength和maxLength:
var cell : BillerTableViewCell?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
cell = tableView.dequeueReusableCell(withIdentifier: "textfieldCell", for: indexPath) as? BillerTableViewCell
cell?.searchTextfield.delegate = self
if self.rowCount! - 1 >= indexPath.row {
if let customerDetail = selectedBiller?.bcustomerparms[indexPath.row] {
alertParam = customerDetail.paramName
cell?.searchTextfield.text = alertParam
if var jsonMin: String = customerDetail.minLength {
alertMinL = Int(jsonMin)
}
if var jsonMax: String = customerDetail.maxLength {
alertMaxL = Int(jsonMax)
}
cell?.searchTextfield.addTarget(self, action: #selector(searchPhoneEditingChanged(textField:)), for: .editingChanged)
} else {
print("no tf")
cell?.searchTextfield.text = "missing"
}
} else {
cell?.searchTextfield.text = "Amount"
hasFinalSearchValueAmnt = true
cell?.searchTextfield.placeholder = "Amount"
cell?.searchTextfield.addTarget(self, action: #selector(searchAmountEditingChanged(textField:)), for: .editingChanged)
}
}
return cell!
}
@objc func searchPhoneEditingChanged(textField: UITextField) {
finalSearchValue = textField.text!
self.textCount = self.finalSearchValue.count
}
@objc func searchAmountEditingChanged(textField: UITextField) {
finalSearchValueAmnt = textField.text!
}
下面我正在做验证,但是如果有三行和三个不同的alertParam
值,但是一直都只有最后一个alertParam jsonMin和jsonMax,为什么?
@objc func buttonClicked(sender: UIButton) {
if self.finalSearchValue.isEmpty{
AlertFun.ShowAlert(title: "", message: "Please enter \(self.alertParam ?? "")", in: self)
} else if self.textCount ?? 0 < self.alertMinL ?? 0 {
AlertFun.ShowAlert(title: "", message: "\(self.alertParam ?? "") Not Less Than \(self.alertMinL ?? 0)", in: self)
} else if self.textCount ?? 0 > self.alertMaxL ?? 0 {
AlertFun.ShowAlert(title: "", message: "\(self.alertParam ?? "") Not More Than \(self.alertMaxL ?? 0)", in: self)
} else if self.finalSearchValueAmnt.isEmpty && hasFinalSearchValueAmnt {
AlertFun.ShowAlert(title: "", message: "Please enter Amount", in: self)
} else {
billerFetchService()
}
}
我如何获得每一行的文本字段的最小长度和最大长度。 请在上面的代码中帮助我。
您问为什么只打印最后一个alertParam
。 好吧,这是因为alertParam
是一个对象(我不知道哪种对象),并且每次在cellForRowAt
绘制一个单元格时都要替换它。 其他所有变量也是如此。
因此,您的UICollectionView将布局所有单元格,然后alertParam
值将是最后一个绘制单元格的值。
在这里,我假设您在整个视图中只有一个按钮,一旦按下该按钮便会打印所有值。 也许您想要每个单元格中的一个按钮,则必须更改此按钮。
如果要打印所有值,则必须使用一个数组,该数组存储在cellForRowAt
中使用的所有alertParam
值。 在代码中,类似于:
let arrayCustomersDetail = []
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
[...]
if let customerDetail = selectedBiller?.bcustomerparms[indexPath.row] {
arrayCustomersDetail.append(customerDetail)
alertParam = customerDetail.paramName
[...]
}
[...]
}
然后,当您单击按钮时,将迭代数组并打印出每个值。
@objc func buttonClicked(sender: UIButton) {
arrayCustomersDetail.forEach {
// Print your stuff
print("Param name: \($0.paramName), minLength: \($0.minLenght)")
}
}
另外,请记住,一次只能显示一个警报。 我确实不知道AlertFun.ShowAlert
做什么,但是您可能无法以这种方式显示多个值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.