How to fetch data displayed in a TableView cell and display it in a textfield?
我在视图控制器中有一个tableView,它在其中显示来自API调用的数据,现在如果用户选择一个tableview单元格(例如TableView中的第2行),我想获取该数据(TableView第2行中的数据) ),并在同一屏幕上的"文本字段"中显示它。做这个的最好方式是什么?
最简单的方法是可以在tableViewCell的
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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中。
1 2 3 4 5 6 7 8 | func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let cell= tableView1.cellForRow(at: indexPath) if let txtVal = cell?.textLabel?.text { textField.text = txtVal } } |
您有来自Webservice的数据,您正在UITableview上显示这些数据,例如
例如:
NSArray * arrWebServiceData; //您正在此处存储来自Wevserice的数据。
func tableView(_ tableView:UITableView,didSelectRowAt indexPath:IndexPath){
1 2 3 4 5 | 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. } |