点击按钮时如何添加行文本字段文本?

How can I get added row textfield text when tapping button?

我在 table 视图中为特定类别添加了额外的行,我需要在点击按钮时添加行文本字段 finalSearchValue。

我有两个文本字段 cell?.searchTextfield.text = alertParam(这是从 json 获得的手机号码)和 cell?.searchTextfield.text = "Amount"(添加了额外的行数)如果我在这两个文本字段中输入我需要的文本两个变量中的两个值,在 payButton 中如何实现。

我是这样拍的;

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

如果我在 finalSearchValueAmnt 中输入金额值,在 finalSearchValue 中输入 phnum,我需要这两个值,但我只得到最后一个文本字段值,为什么?请帮助。

代码如下:

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 searchEditingChanged(textField: UITextField) {
    finalSearchValue = textField.text!
    self.textCount = self.finalSearchValue.count
}

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

如何在一个变量中获得 cell?.searchTextfield.text = "Amount" finalSearchValue,请在上面的代码中帮助我。

问题:如何从文本字段中获取值。

解决方案:我创建了两个操作方法。一个用于 Phone 数量,第二个用于数量。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

if indexPath.section == 0 {

    cell = tableView.dequeueReusableCell(withIdentifier: "textfieldCell", for: indexPath) as? BillerTableViewCell

    if self.rowCount! - 1 >= indexPath.row
    {
        if let customerDetail = selectedBiller?.bcustomerparms[indexPath.row] {

            alertParam = customerDetail.paramName
            cell?.searchTextfield.text = alertParam 
    cell?.searchTextfield.addTarget(self, action: #selector(searchPhoneEditingChanged(textField:)), for: .editingChanged) // for phone number 

       }

    }
    else{
        cell?.searchTextfield.text = "Amount"
    cell?.searchTextfield.addTarget(self, action: #selector(searchAmountEditingChanged(textField:)), for: .editingChanged) // for amount

    }
} 
return cell!
 }

// 动作方法

@objc func searchPhoneEditingChanged(textField: UITextField) {
    finalSearchValue = textField.text! // used to store phone Number
    self.textCount = self.finalSearchValue.count
}

@objc func searchAmountEditingChanged(textField: UITextField) {
    finalSearchValueAmnt = textField.text! // used to store Amount.
}

// 检索两个值

@objc func buttonClicked(sender: UIButton) {
  //Now here you can get both the values.
  print(finalSearchValueAmnt)
  print(finalSearchValue)

 }