【问题标题】:How can I get added row textfield text when tapping button?点击按钮时如何获得添加的行文本字段文本?
【发布时间】:2020-03-19 21:45:06
【问题描述】:

我在表格视图中为特定类别添加了额外的行,我需要在点击按钮时添加行文本字段 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,请在上面的代码中帮助我。

【问题讨论】:

  • 能不能尽量减少代码,只剩下你遇到的问题?
  • @TomášPospíšek,我已经编辑了我的问题,请在代码中帮助我
  • 单元格仍未定义。如果它是在let cell = tableView.deque... 中定义的,它就不是可选的,也不需要可选链cell?.。您需要一个单元格来返回您正在执行的任何操作,因此请在方法中执行任何其他操作之前创建它。
  • 我认为查看 tableCell 的外观对于了解您要实现的目标也很有帮助。我个人无法想象,你想要达到的究竟是什么。
  • @SwiftIos 检查我的答案。

标签: ios swift uitableview uitextfield


【解决方案1】:

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

解决方案:我创建了两个操作方法。一个是电话号码,第二个是金额。

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)

 }

【讨论】:

  • 一次请看我的buttonClicked 我在这里做错了什么,如果没有添加行意味着只有一行那么它也说请输入金额,为什么?
  • 如果只有一行那么我不需要金额
猜你喜欢
  • 1970-01-01
  • 2015-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多