【发布时间】:2017-02-10 06:11:15
【问题描述】:
我在视图控制器中有一个 tableView,我在其中显示来自 API 调用的数据,现在如果用户选择一个 tableview 单元格(比如 TableView 中的第 2 行),我想获取该数据(TableView 第 2 行中的数据) 并将其显示在同一屏幕上的文本字段中。做这个的最好方式是什么?
【问题讨论】:
标签: ios swift uitableview
我在视图控制器中有一个 tableView,我在其中显示来自 API 调用的数据,现在如果用户选择一个 tableview 单元格(比如 TableView 中的第 2 行),我想获取该数据(TableView 第 2 行中的数据) 并将其显示在同一屏幕上的文本字段中。做这个的最好方式是什么?
【问题讨论】:
标签: ios swift uitableview
最简单的方法是在tableViewCell 的didSelect 上显示UIAlertController 和textField。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let alert = UIAlertController(title: "Edit Data", message: "Edit corresponding detail", preferredStyle: .alert)
alert.addTextField { (textField) in
textField.placeholder = "Enter name"
textField.text = yourArray[indexPath.row]
}
alert.addAction(UIAlertAction(title: "Edit", style: .default, handler: { (action) in
//Edit data on array
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true)
}
【讨论】:
你可以试试这个,当用户选择特定的 UITableViewCell 时,单元格内容将显示在 UITextField 中。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell= tableView1.cellForRow(at: indexPath)
if let txtVal = cell?.textLabel?.text {
textField.text = txtVal
}
}
【讨论】:
您有来自 web 服务的数据,这些数据显示在 UITableview 上,例如 arrWebServiceData。你有用户点击的行索引说index。最简单的方法是在index 处从arrWebServiceData 获取数据并使用它显示在测试字段上。
例如:
NSArray *arrWebServiceData; //你在这里存储来自 Wevserice 的数据。
func tableView(_tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let index = indexPath.row;
let dataAtIselectedIndex = yourArray[indexPath.row] ;
//If data is string, dirctly display it on text field,or if its dictionary, set object of diction on your text field.
}
【讨论】: