2017-02-10 5 views
2

ユーザーがTableviewセル(TableViewの行2)を選択したときに、そのデータ(TableViewの2行目のデータ)を取得したい場合は、API呼び出しのデータを表示するビューコントローラにtableViewがあります。同じ画面のテキストフィールドに表示します。これを行う最善の方法は何ですか?TableViewセルに表示されたデータをフェッチしてテキストフィールドに表示する方法は?

答えて

4

あなたは、セルの内容は、UITextFieldの中のショーになることを、この1、ユーザーが選択するの特定のUITableViewCellを試すことができます。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

     let cell= tableView1.cellForRow(at: indexPath) 

     if let txtVal = cell?.textLabel?.text { 
      textField.text = txtVal 
     } 
    } 
+0

これは完璧に動作します! – SeaWarrior404

+0

ハッピーコーディング:-) –

5

最も簡単な方法は、UIAlertControllertextFieldと表示することができます。これは、tableViewCellのdidSelectにあります。

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) 
} 
0

あなたはあなたのUITableViewに表示されているWebサービスからのデータを持っているがarrWebServiceDataを言います。ユーザーがクリックした行のインデックスがindexとなっています。最も簡単な方法は、データをarrWebServiceDataからindexに取得し、それをテストフィールドに表示することです。例えば

に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. 

} 
関連する問題