Unable to validate added row tableview textField in swift

走远了吗. 提交于 2019-12-13 03:25:36

问题


For all categories i am getting one value from json but For one category i need extra row in tableview, i have added extra row and text in textfield but i am unable to validate its textfield text value.

this code:

  var finalSearchValue : String = ""
  var finalSearchValueAmnt : String = ""
  var cell : BillerTableViewCell?

added extra row for bcategoryname == "Mobile Postpaid"

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

if section == 0 {

    self.rowCount = selectedBiller?.bcustomerparms.count ?? 0
    if selectedBiller?.bcategoryname == "Mobile Postpaid"{

        return  self.rowCount! + 1
    }
    else{
        return selectedBiller?.bcustomerparms.count ?? 0
    }
}
return 1
}

for all categories i am getting mobile number from json to textfield but for extra row i have added amount textfield

 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 priceOfProduct: String = customerDetail.minLength {
                alertMinL = Int(priceOfProduct)
            }
            if var priceOfProduct: String = customerDetail.maxLength {
                alertMaxL = Int(priceOfProduct)
            }
            cell?.searchTextfield.addTarget(self, action: #selector(searchPhoneEditingChanged(textField:)), for: .editingChanged)
        }
        else{
            print("no tf")
            cell?.searchTextfield.text = "missing"
        }
    }
    else{
        cell?.searchTextfield.text = "Amount"
        cell?.searchTextfield.addTarget(self, action: #selector(searchAmountEditingChanged(textField:)), for: .editingChanged)

    }
}
return cell!
}

here getting both textfield finalvalues

@objc func searchPhoneEditingChanged(textField: UITextField) {
finalSearchValue = textField.text!
self.textCount = self.finalSearchValue.count
}

@objc func searchAmountEditingChanged(textField: UITextField) {
finalSearchValueAmnt = textField.text!
}

in this is button action:

if there is no extra row then also it says please enter amount alert, if there no added row then i dont need enter amount alert, i want enter amount alert only if there is added row

  @objc func buttonClicked(sender: UIButton) {
    //let buttonRow = sender.tag
    print("in newbiller search button")
    print("the amount value \(finalSearchValueAmnt)")
    print("the phone value \(finalSearchValue)")

    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 lessthen \(self.alertMinL!)", in: self)
    }
    else if self.textCount ?? 0 > self.alertMaxL ?? 0{
        AlertFun.ShowAlert(title: "", message: "\(self.alertParam!) not graterthen \(self.alertMaxL!)", in: self)
    }
    if self.finalSearchValueAmnt.isEmpty{
        AlertFun.ShowAlert(title: "", message: "Please enter Amount", in: self)
    }
    else{
    billerFetchService()
    }
}

please help me in the above code, to validate added row textfield.


回答1:


You have set the variable finalSearchValueAmnt to "" so it doesn't matter if the user didn't add a value in the text field or that row, where you add the text field is existing or not.

The below might solve your problem, but you should still consider your approach for your application's view

Add a variable var hasFinalSearchValueAmnt : Bool = false

in cellForRowAt set this variable to true if this Textfield is shown.

Then in your check function, only show the alert if the Textfield was added and is empty:

if self.finalSearchValueAmnt.isEmpty && hasFinalSearchValueAmnt {
        AlertFun.ShowAlert(title: "", message: "Please enter Amount", in: self)
    }

I didn't try this out in Xcode, as there are still missing too many information from your side, how your view should be working. Also, will only work if there are not other rows added dynamically.



来源:https://stackoverflow.com/questions/59019652/unable-to-validate-added-row-tableview-textfield-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!