繁体   English   中英

在自定义单元格TableView中选择了哪个TextField?

[英]Which TextField was selected in custom-Cell TableView?

使用Swift4,iOS11.1,Xcode9.1,

从tableView进行搜索,我试图弄清楚如何检测用户触摸了两个TextField(在我的自定义单元格中)中的哪个。 我的custom-Cell tableView有两个TextField,如下图所示:

在此处输入图片说明

到目前为止,这是我的代码:

// tableView delegation-method:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)
    self.performSegue(withIdentifier: "goToMyNextVC", sender: cell)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "goToMyNextVC" {

        let newVC = segue.destination as! MyNextViewController

        let indexPath = self.tableView.indexPathForSelectedRow
        print(indexPath?.row)  // !!!!!! The cell-row is correct

        let cell = sender as! MyCustomTableViewCell
        newVC.someTextProperty1 = cell.firstTextFiled.text!  // works well
        newVC.someTextProperty2 = cell.secondTextFiled.text!  // works well

        // !!!! BUT HOW DO I GET WHICH OF THE TWO TEXTFIELDS WAS TOUCHED ?????????

        // newVC.someTextProperty3 = ??????? text of touched TextField ???????

    }
}

任何帮助表示赞赏!

您应该在MyCustomTableViewCell类内的textField上注册一个tap事件,然后将哪个textField分配回您的viewController。

但是更简单的解决方案是将两个不同的单元格用于两个不同的文本字段。

我找到了一个解决方案(某种程度上是由Demosthese提示触发的):

1.)在自定义单元中:调用一个单元配置方法(分配每个textField的标签(或在我的情况下,即刻标记……)

func configureCell(tag: Int) {

    // assign tag (later used to expand / collapse the right cell)
    self.fristTextField.tag = 1
    self.secondTextField.tag = 2

    self.firstTextField.isUserInteractionEnabled = true
    self.secondTextField.isUserInteractionEnabled = true
}
  1. 在自定义单元内:定义一个touchesBegan方法,以响应被触摸的标签并分配“ tagNrTouched”属性

     override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { super.touchesBegan(touches, with: event) let touch: UITouch = touches.first! if (touch.view?.tag == self.firstTextField.tag) { self.tagNrTouched = 1 } else if (touch.view?.tag == self.secondTextField.tag) { self.tagNrTouched = 2 } } 
  2. 在tableView内部,在segue'ing之前对此属性做出反应:

     // tableView delegation-method: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let cell = tableView.cellForRow(at: indexPath) self.performSegue(withIdentifier: "goToMyNextVC", sender: cell) } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "goToMyNextVC" { let newVC = segue.destination as! MyNextViewController let indexPath = self.tableView.indexPathForSelectedRow print(indexPath?.row) // !!!!!! The cell-row is correct let cell = sender as! MyCustomTableViewCell newVC.someTextProperty1 = cell.firstTextFiled.text! // works well newVC.someTextProperty2 = cell.secondTextFiled.text! // works well // HERE IS THE FINAL SOLUTION: REACTING ACCORDING TO THE TAG-NR if (cell.tagNrTouched == 1) { textSearchVC.searchText = cell.firstTextField.text! } else if (cell.tagNrTouched == 2) { textSearchVC.searchText = cell.secondTextField.text! } } } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM