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

时间:2019-02-13 09:37:59

标签: ios swift

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

UITableViewCell类:

class AddCustomerCell: UITableViewCell {


@IBOutlet weak var textfield: SkyFloatingLabelTextField!

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

}

UITableView数据源和委托方法:-

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)
        }

ViewController中选择了textField时,我想打开新的indexPath.row == 3。我怎样才能做到这一点。请帮忙吗?

5 个答案:

答案 0 :(得分: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)
    }
}

我希望这会对您有所帮助。

答案 1 :(得分:0)

与其在viewController中推送textFieldDidEndEditing,而不是在textfieldShouldBeginEditing中写,就像当您点击textField时,第一个被呼叫的代表是textFieldShouldBeginEditing

extension AddCustomerViewController : UITextFieldDelegate {
    func textFieldShouldBeginEditing(_ textField: UITextField) {
        if let indexPath = self.tableView.indexPath(forItem: textField) {
            if indexPath.row == 3 {
                //asm
                self.areaSalesMen = textField.text ?? ""
                let vc = AreaSalesMenListViewController.instantiate(fromAppStoryboard: .Main)
                self.navigationController?.pushViewController(vc, animated: true)
            }
        }
    }
}

答案 2 :(得分:0)

textFieldShouldBeginEditing中添加要导航的代码,然后返回false。

textFieldShouldBeginEditing是第一个被调用的方法,它确定textField是否应该开始编辑,因为您不希望用户输入任何文本,因此可以从此委托方法返回false并执行导航

答案 3 :(得分:0)

在cellForRow中:

if indexPath.row == 3 {
    textfield.isUserInteractionEnabled = false
}

和didSelect中:

if indexPath.row == 3 {
   // Push your controller
}

答案 4 :(得分:0)

检查UITextFieldDelegate。

尤其是textFieldDidBeginEditing函数。

此功能告诉委托人编辑是在指定的文本字段中开始的。

因此将您的代码放入该函数中。