无法快速验证添加的行tableview textField [英] Unable to validate added row tableview textField in swift

查看:50
本文介绍了无法快速验证添加的行tableview textField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于所有类别,我都从json获得一个值,但是对于一个类别,我需要在表视图中增加一行,我在文本字段中添加了额外的行和文本,但是我无法验证其文本字段的文本值.

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.

此代码:

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

bcategoryname ==移动后付费"

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
}

对于所有类别,我都从json到文本字段获取手机号码,但对于额外的行,我添加了数量的文本字段

 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!
}

这是按钮操作:

如果没有多余的行,那么它还说请输入金额警报,如果没有添加的行,那么我就不需要输入金额警报,我只想在添加行的情况下输入金额警报

  @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.

推荐答案

您已将变量 finalSearchValueAmnt 设置为" ,因此用户是否输入不要在文本字段或添加文本字段的行中添加值.

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.

以下内容可能会解决您的问题,但您仍应考虑应用程序视图的方法

添加变量 var hasFinalSearchValueAmnt:Bool = false

将此变量设置为 true .

然后在检查功能中,仅在添加了文本字段且为空的情况下才显示警报:

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

我没有在Xcode中进行尝试,因为您身边仍然缺少太多信息,这些信息说明了视图的工作方式.而且,仅在没有动态添加其他行的情况下才起作用.

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.

这篇关于无法快速验证添加的行tableview textField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆