[英]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.