无法验证 swift 中添加的行表视图文本字段
Unable to validate added row tableview textField in swift
对于所有类别,我从 json 中获得一个值,但对于一个类别,我需要在表视图中添加额外的行,我在文本字段中添加了额外的行和文本,但我无法验证其文本字段文本值。
此代码:
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 中尝试这个,因为您这边仍然缺少太多信息,您的视图应该如何工作。
此外,只有在没有动态添加其他行时才有效。
对于所有类别,我从 json 中获得一个值,但对于一个类别,我需要在表视图中添加额外的行,我在文本字段中添加了额外的行和文本,但我无法验证其文本字段文本值。
此代码:
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 中尝试这个,因为您这边仍然缺少太多信息,您的视图应该如何工作。 此外,只有在没有动态添加其他行时才有效。