json - 为什么我在快速解析时没有在按钮操作中获取所有 cellForRowAtindexPath 文本字段值

标签 json swift uitableview button textfield

在cellForRowAtindexPath中,我得到所有alertParam,alertMinL值,这意味着如果有一个alertParam,那么我将得到一个alertParam,如果有三个alertParam,那么我将得到alertParam值,甚至在tableview中,但同时我正在按钮中进行验证,然后我没有获得所有三个值,我只获得一个alertParam和最小值、最大值,为什么?我如何验证表格 View 中的所有三个文本字段?

在类里面声明:

var alertMinL: Int?
var alertMaxL: Int?
var alertParam: String?

cellForRowAt indexPath 中的代码:这里我根据 Json 获取所有值:

 if let customerDetail = selectedBiller?.bcustomerparms[indexPath.row] {
            alertParam = customerDetail.paramName
            cell?.searchTextfield.text = alertParam
            print("cell param values \(alertParam)")
            if var priceOfProduct: String = customerDetail.minLength {
                alertMinL = Int(priceOfProduct)
                print("cell minLen values \(alertMinL)")
            }
            if var priceOfProduct: String = customerDetail.maxLength {
                alertMaxL = Int(priceOfProduct)
            }
            cell?.searchTextfield.addTarget(self, action: #selector(searchPhoneEditingChanged(textField:)), for: .editingChanged)
        }

在buttonaction中,即使有树paramName,我也只能得到一个值。如何在此处验证所有三个文本字段。

 @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 Less Than \(self.alertMinL ?? 0)", in: self)
}
else if self.textCount ?? 0 > self.alertMaxL ?? 0{
    AlertFun.ShowAlert(title: "", message: "\(self.alertParam ?? "") Not More Than \(self.alertMaxL ?? 0)", in: self)
}
else if self.finalSearchValueAmnt.isEmpty && hasFinalSearchValueAmnt{
    AlertFun.ShowAlert(title: "", message: "Please enter Amount", in: self)
}
else{
    billerFetchService()
}
}

这里的finalSearchValue是:

@objc func searchPhoneEditingChanged(textField: UITextField) {
    finalSearchValue = textField.text!
    self.textCount = self.finalSearchValue.count
}

最佳答案

要对所有单元格运行测试,您需要迭代它们。如果您确信它们始终可见,那么您可以使用 tableView 的 visibleCells 属性,但通过特定的索引路径获取对它们的引用会更安全。

更改 buttonClicked 方法以执行以下操作...

首先创建要测试的indexPaths 数组。这假设它们都在第 0 部分中并且您想要测试每个单元格。如果没有,您将需要相应地调整

let count = tableView.numberOfRows(inSection: 0)
let indexPaths = (0..<count).map{IndexPath(row: $0, section: 0)}

迭代此索引路径数组,检索单元格,并针对它运行验证代码:

for indexPath in indexPaths {
 let cell = tableView.cellForRow(at: indexPath) as! YourTableCellType
 if cell.finalSearchValue.isEmpty{
    AlertFun.ShowAlert(title: "", message: "Please enter \(self.alertParam ?? "")", in: self)
 } else if let textCount = textCount, let alertMinL = alertMinL, textCount < alertMinL {
    AlertFun.ShowAlert(title: "", message: "\(self.alertParam ?? "") Not Less Than \(self.alertMinL ?? 0)", in: self)
 } else if let textCount = textCount, let alertMaxL = alertMaxL, textCount > alertMaxL {
    AlertFun.ShowAlert(title: "", message: "\(self.alertParam ?? "") Not More Than \(self.alertMaxL ?? 0)", in: self)
 } else if self.finalSearchValueAmnt.isEmpty && hasFinalSearchValueAmnt{
    AlertFun.ShowAlert(title: "", message: "Please enter Amount", in: self)
 } else{
    billerFetchService()
 }
}

注意:我已经解开你的可选变量,而不是用 nil 合并它们,因为我更喜欢它的风格。

关于json - 为什么我在快速解析时没有在按钮操作中获取所有 cellForRowAtindexPath 文本字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59103294/

相关文章:

javascript - JSON 和 JavaScript - 如何使用 json 读取我的列表

java - Jackson 未知字段的 json 数组列表

arrays - 嵌套数组结构的 JMESPath 查询

iphone - 如何从分组部分 uitableview 添加/删除单元格?

javascript - 从 API 获取 Json 以在 Gmap 中使用

ios - 在 ObjC 中工作,而不是在 swift 中工作。我在 swift 中使用 JSON 解析有什么问题

objective-c - 发布版本中 rawValue 的重新声明无效

ios - 单击 View 时出现导航栏

objective-c - 取消本地通知不起作用

ios - 从 XIB 调用函数时 tableView 为 nil