簡體   English   中英

點擊按鈕時如何獲得添加的行文本字段文本?

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

我在表視圖中為特定類別添加了額外的行,我需要在點擊按鈕時添加行文本字段 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,請在上面的代碼中幫助我。

問題:如何從文本字段中獲取值。

解決方案:我創建了兩個操作方法。 第一個是電話號碼,第二個是金額。

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)

 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM