【问题标题】:Which TextField was selected in custom-Cell TableView?在 custom-Cell TableView 中选择了哪个 TextField?
【发布时间】:2017-12-17 19:28:16
【问题描述】:

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

从 tableView 进行 segue,我试图弄清楚如何检测用户触摸了两个 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 ???????

    }
}

任何帮助表示赞赏!

【问题讨论】:

    标签: ios swift tableview segue custom-cell


    【解决方案1】:

    您应该在 MyCustomTableViewCell 类中的 textField 上注册一个点击事件,然后将被点击的 textField 委托给您的 viewController。

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

    【讨论】:

    • 感谢您的两个想法 - 我会试一试!
    【解决方案2】:

    我找到了一个解决方案(不知何故由 Demosthese 提示触发):

    1.) 在自定义单元格内部:调用单元格配置方法(分配每个文本字段的标签(或现在在我的情况下准确地分配标签...)

    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 之前对该属性做出反应:

      // 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!
              }
          }
      }
      

    【讨论】:

    • 你可以在 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) 方法中将标签分配给整个单元格而不是单个元素,然后使用委托方法发送。
    • 我想区分在一个自定义单元格中触摸了哪个元素 - 因此分配给单个元素(而不是整个单元格)。或者你有你的意思的代码示例吗?
    • 抱歉没有发现问题...我有点困惑为什么不使用按钮和委托来查看控制器,而不是在单击时使用文本字段?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    • 2020-06-03
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    相关资源
    最近更新 更多