簡體   English   中英

無法快速驗證添加的行 tableview textField

[英]Unable to validate added row tableview textField in swift

對於所有類別,我從 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()
    }
}

請在上面的代碼中幫助我,以驗證添加的行文本字段。

您已將變量finalSearchValueAmnt設置為""因此用戶是否沒有在文本字段或該行中添加值都沒有關系,您添加文本字段的位置是否存在。

以下內容可能會解決您的問題,但您仍應考慮應用程序視圖的方法

添加變量var hasFinalSearchValueAmnt : Bool = false

如果顯示此文本字段,則在cellForRowAt將此變量設置為true

然后在您的檢查功能中,如果文本字段已添加且為空,則僅顯示警報:

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

我沒有在 Xcode 中嘗試過這個,因為你這邊仍然缺少太多信息,你的視圖應該如何工作。 此外,只有在沒有動態添加其他行時才有效。

暫無
暫無

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

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