Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在自定义单元格表视图中选择了哪个文本字段?_Ios_Swift_Tableview_Segue_Custom Cell - Fatal编程技术网

Ios 在自定义单元格表视图中选择了哪个文本字段?

Ios 在自定义单元格表视图中选择了哪个文本字段?,ios,swift,tableview,segue,custom-cell,Ios,Swift,Tableview,Segue,Custom Cell,使用Swift4、iOS11.1、Xcode9.1、 从tableView中进行一段,我试图找出如何检测用户触摸了两个文本字段(在我的自定义单元格中)中的哪一个。我的自定义单元格表格视图有两个文本字段,如下图所示: 以下是我目前的代码: // tableView delegation-method: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let cell = tab

使用Swift4、iOS11.1、Xcode9.1、

从tableView中进行一段,我试图找出如何检测用户触摸了两个文本字段(在我的自定义单元格中)中的哪一个。我的自定义单元格表格视图有两个文本字段,如下图所示:

以下是我目前的代码:

// 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上注册一个点击事件,然后将点击的textField委托回viewController

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

我找到了一个解决方案(以某种方式由演示提示触发):

1.)在自定义单元格内:调用单元格配置方法(分配每个文本字段的标记(或者在我的示例中是acually标签…)

  • 在自定义单元格内:定义一个touchesbreated方法,该方法对标记taht is toucted作出反应,并指定一个“tagnrtoched”-属性


  • 感谢这两个想法-我来试一试!您可以将标记分配给整个单元格,而不是方法func tableView中的单个元素(tableView:UITableView,cellForRowAt indexath:indexPath)当退出队列,然后用委托方法发送它时。我想区分在一个自定义单元格中触碰了哪个元素-因此分配给单个元素(而不是整个单元格)。或者您是否有一个代码示例来说明您的意思?很抱歉,没有遇到问题…我有点困惑,为什么单击时不使用按钮和委托来查看控制器而不是文本字段?
    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
    }
    
    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
        }
    }
    
    // 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!
            }
        }
    }