【发布时间】:2020-03-20 00:37:13
【问题描述】:
对于所有类别,我从 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()
}
}
请在上面的代码中帮助我,以验证添加的行文本字段。
【问题讨论】:
-
请告诉我们发生了什么以及您希望发生什么,以便我们更好地帮助您
-
这里使用TableView的原因是什么?我感觉,使用 ScrollView 和 VerticalStackView 可能会更好?
-
@Chris,我已经编辑了问题,请在代码中提供帮助
-
@Maverick2805,我是ios新手,不知道verticalverticalstackview的概念,我已经编辑了问题,请在代码中帮助我
-
这似乎与您今天早上发布的问题重复,并且仍然表现出与该帖子相同的问题和困惑。正如您自己所说,您是新手,不了解 iOS 开发的一些基本原则。我认为您最好花时间做一些教程,而不是复制和粘贴其他人为您编写的代码 sn-ps。从长远来看,您花费的时间会获得更好的价值。如果这听起来很苛刻,我们深表歉意,但这是出于好意。
标签: ios swift uitableview if-statement uitextfield