如何从 UItableView 中获取 textField 的值? [关闭]

Posted

技术标签:

【中文标题】如何从 UItableView 中获取 textField 的值? [关闭]【英文标题】:How to get value from UItableView for textField? [closed] 【发布时间】: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您的问题提供所有相关细节。 【参考方案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 

【讨论】:

以上是关于如何从 UItableView 中获取 textField 的值? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

如何从 UITableView 中获取对象 id 并根据对象 id 删除对象?

在 Swift 中,如何从 UITableView 中获取对象 id 并根据对象 id 删除对象?

如何从 UItableView 中获取 textField 的值? [关闭]

如何从领域列表中获取要在 UITableview 中使用的值

如何从 UITableView 获取行索引,其中数组列表由追加到类

如何从获取的结果对象中填充稀疏的 UITableView?