简体   繁体   中英

How to validate each row textfield in tableview in swift

Here i am getting each row textfield text, its minLength and 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!
}

below i am doing validation but if there are three rows and three different alertParam values but all the time i am getting only last alertParam jsonMin and jsonMax why?


@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()
   }
}

how can i get each row textfield minlength and maxlength. please help me in the above code.

You ask why only the last alertParam is being printed. Well, that's because alertParam is an object (I don't know what kind of object), and you're replacing it every time you draw a cell in cellForRowAt . The same goes for all your other variables.

So, your UICollectionView will layout all the cells, and then alertParam value will be the value of the last draw cell.

Here I'm assuming you only have one button for the whole view that once pressed prints all the values. Maybe you want one button in each cell, you'd have to change this.

If you want to print all the values, you'll have to use an array that stores all the alertParam values which are used in cellForRowAt . In code, something like:

let arrayCustomersDetail = []
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    [...]
    if let customerDetail = selectedBiller?.bcustomerparms[indexPath.row] {
        arrayCustomersDetail.append(customerDetail)
        alertParam = customerDetail.paramName
        [...]
    }
    [...]
}

And when you click the button, you iterate the array and print out every value.

@objc func buttonClicked(sender: UIButton) {
    arrayCustomersDetail.forEach {
        // Print your stuff
        print("Param name: \($0.paramName), minLength: \($0.minLenght)")
    }
}

Also, keep in mind that you only can show one Alert at a time. I don't know what AlertFun.ShowAlert does, exactly, but you might not be able to show multiple values that way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM