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

时间:2019-11-24 12:26:55

标签: ios swift uitableview uitextfield

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

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

enter image description here

我这样子;

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,请在上面的代码中为我提供帮助。

1 个答案:

答案 0 :(得分:0)

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

解决方案:我已经创建了两种方法。一个代表电话号码,第二个代表金额。

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)

 }