【发布时间】: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