gpt4 book ai didi

ios - 在 tableView Swift 中选择 textField 时打开 New ViewController

转载 作者:行者123 更新时间:2023-12-01 18:04:24 25 4
gpt4 key购买 nike

我在 tableView 中使用一个 textField 并通过使用 indexPath.row 获取不同的数据但问题是我正在尝试打开一个新的ViewControllertableView 中选择特定文本字段时.

UITableViewCell 类:

class AddCustomerCell: UITableViewCell {


@IBOutlet weak var textfield: SkyFloatingLabelTextField!

override func awakeFromNib() {
super.awakeFromNib()
}

}

UITableView 数据源和委托(delegate)方法:-
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return 3
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

guard let cell = self.tableView.dequeueReusableCell(withIdentifier: "customerCell", for: indexPath) as? AddCustomerCell else {
return UITableViewCell()
}
cell.textfield.delegate = self
cell.textfield.placeholder = fieldArr[indexPath.row]["title"]
cell.textfield.title = fieldArr[indexPath.row]["title"]
cell.selectionStyle = .none

if indexPath.row == 0 {

cell.textfield.text = self.fullName
cell.textfield.keyboardType = .default
}
else if indexPath.row == 1 {

cell.textfield.text = self.email
cell.textfield.keyboardType = .emailAddress
}

else if indexPath.row == 2 {
cell.textfield.text = self.phoneNumber
cell.textfield.keyboardType = .numberPad
}
else if indexPath.row == 3 {
cell.textfield.text = self.areaSalesMen
cell.textfield.keyboardType = .default
}

else {
cell.textfield.isSecureTextEntry = false

}

return cell
}

UITableView didSelect 行方法
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let indexPath = tableView.indexPathForSelectedRow!
print("index \(indexPath.row)")

if indexPath.row == 0 {
print("full name is selceted")
}
else if indexPath.row == 1 {

}
else if indexPath.row == 2 {

}
else if indexPath.row == 3 {
let vc = AreaSalesMenListViewController.instantiate(fromAppStoryboard: .Main)
self.navigationController?.pushViewController(vc, animated: true)
}

这是 UItextField 扩展:-
extension AddCustomerViewController: UITextFieldDelegate {

func textFieldDidEndEditing(_ textField: UITextField) {
if let indexPath = self.tableView.indexPath(forItem: textField) {
if indexPath.row == 0 {
//firstName
self.fullName = textField.text ?? ""

}
else if indexPath.row == 1 {
//lastName
self.email = textField.text ?? ""

}
else if indexPath.row == 2 {
//userId
self.phoneNumber = textField.text ?? ""

}
else if indexPath.row == 3 {
//asm
self.areaSalesMen = textField.text ?? ""
let vc = AreaSalesMenListViewController.instantiate(fromAppStoryboard: .Main)
self.navigationController?.pushViewController(vc, animated: true)
}

我要开新 ViewControllertextFieldindexPath.row == 3 中选择.我怎样才能做到这一点。请帮忙?

最佳答案

cellForRow方法添加以下行来设置 textField 的标签:

cell.textfield.tag = indexPath.row

textFieldDidBeginEditing添加以下代码以检测第 4 行中选定的 textField 表示 indexPath.row == 3 .
func textFieldDidBeginEditing(_ textField: UITextField) {

if textField.tag == 3 {

textField.resignFirstResponder()

self.areaSalesMen = textField.text ?? ""
let vc = AreaSalesMenListViewController.instantiate(fromAppStoryboard: .Main)
self.navigationController?.pushViewController(vc, animated: true)
}
}

我希望这能帮到您。

关于ios - 在 tableView Swift 中选择 textField 时打开 New ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54666847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com