【问题标题】:Unable to validate added row tableview textField in swift无法快速验证添加的行 tableview textField
【发布时间】:2020-03-20 00:37:13
【问题描述】:

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

这段代码:

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

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
}

对于所有类别,我将手机号码从 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!
}

在这里获取两个文本字段的最终值

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

请在上面的代码中帮助我,以验证添加的行文本字段。

【问题讨论】:

  • 请告诉我们发生了什么以及您希望发生什么,以便我们更好地帮助您
  • 这里使用TableView的原因是什么?我感觉,使用 ScrollView 和 VerticalStackView 可能会更好?
  • @Chris,我已经编辑了问题,请在代码中提供帮助
  • @Maverick2805,我是ios新手,不知道verticalverticalstackview的概念,我已经编辑了问题,请在代码中帮助我
  • 这似乎与您今天早上发布的问题重复,并且仍然表现出与该帖子相同的问题和困惑。正如您自己所说,您是新手,不了解 iOS 开发的一些基本原则。我认为您最好花时间做一些教程,而不是复制和粘贴其他人为您编写的代码 sn-ps。从长远来看,您花费的时间会获得更好的价值。如果这听起来很苛刻,我们深表歉意,但这是出于好意。

标签: ios swift uitableview if-statement uitextfield


【解决方案1】:

您已将变量finalSearchValueAmnt 设置为"",因此用户是否没有在文本字段或添加文本字段的那一行中添加值并不重要。

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

添加变量var hasFinalSearchValueAmnt : Bool = false

如果显示此文本字段,则在 cellForRowAt 中将此变量设置为 true

然后在您的检查功能中,仅在 Textfield 已添加且为空时显示警报:

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

我没有在 Xcode 中尝试过,因为您仍然缺少太多信息,您的视图应该如何工作。 此外,只有在没有动态添加其他行时才有效。

【讨论】:

  • 你是对的,我有不同的客户参数..根据 paramName 我需要在 tableview 中显示行数。
  • @SwiftIos 我仍然认为,您应该以与 tableView 不同的方式布局视图。也许看看一些 StackView 教程(包括以编程方式使用 StackViews)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-21
  • 1970-01-01
  • 2015-02-18
  • 1970-01-01
相关资源
最近更新 更多