【问题标题】:How to get value from UItableView for textField? [closed]如何从 UItableView 中获取 textField 的值? [关闭]
【发布时间】:2018-02-18 13:56:28
【问题描述】:

我的 viewController 上有 tableView,还有一个 textField。 TableView 显示名称列表。当我单击具有名称的单元格时,我想获得该名称,例如文本字段文本。 有人知道我该怎么做吗?

【问题讨论】:

  • 粘贴代码。那我直接告诉你一个代码
  • func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return names.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) let names = names[indexPath.row] cell.textLabel!.text = imena return cell } }
  • 我的 textField 文本有问题,tableview 看起来不错
  • 检查答案现在把断点打印出来。现在打印特定行的名称
  • @MilicaKnezevic 不要在 cmets 中发布代码。请edit您的问题提供所有相关细节。

标签: ios swift uitableview uitextfield swift4


【解决方案1】:

tableView 中的方法 didSelectRow 将为您提供单击的 indexPath。您可能正在基于数组加载这些名称,然后您可以执行 array[indexPath.row]

【讨论】:

    【解决方案2】:

    将此代码添加到您的课程中。

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        yourTextfield.text = namesArray[indexPath.row] 
    }
    

    【讨论】:

      【解决方案3】:
      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
           let name = names[indexPath.row]
           yourTextfieldName.text = namesArray[indexPath.row]
      }
      

      【讨论】:

        【解决方案4】:

        当用户在didSelectRowAt 中检测到tableView 单元格时,您可以获得选定的单元格索引以在数组中查找名称。

        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
              let name = names[indexPath.row]  // Get selected name from array names 
              print(name)
             textField.text = name // Assign   textField text to selected name 
        }
        

        【讨论】:

          猜你喜欢
          • 2021-07-22
          • 1970-01-01
          • 1970-01-01
          • 2013-09-27
          • 1970-01-01
          • 1970-01-01
          • 2019-01-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多