2016-12-19 5 views
2

私はこのUITableViewセルに4 UITextFieldを持っています。ボタンクリックでその値を取得したいと思います。どのようにUITableViewCellからテキストフィールドの値を取得するには?

このコードでは値は取得されません。

@IBAction func printBtnAction(_ sender: Any) { 

    let cell = self.myTableView.dequeueReusableCell(withIdentifier: "manualAddC1") as! AddFriendC1Cell 

    let alias = cell.aliasTextField.text! 
    let primaryPhone = cell.primaryPhoneTextField.text! 
    let secondaryPhone = cell.seondaryPhoneTextField.text! 
    let email = cell.emailAddressTextField.text! 

    print("Alias: \(alias), Phone: \(primaryPhone), Phone2: \(secondaryPhone), Email: \(email)") 
} 

これは、テーブルビュー enter image description here

+0

さらに詳しい情報が役に立ちます。 –

+0

出力を印刷しますか?どうしましたか ?もっと詳しく説明してください –

+0

テーブルビューのスクリーンショットを入れてください – KAR

答えて

0

OKのスクリーンショットです。ボタンを押すと、tableViewCellをデキューします。

let cell = self.myTableView.dequeueReusableCell(withIdentifier: "manualAddC1") as! AddFriendC1Cell 

私はあなたがこのような既存のtableViewCellへの参照を取得したいと考えてい:

if let cell = tableView.cellForRow(at: indexPath) as? AddFriendC1Cell { 
     // do what you need with cell 
    } 

これが働くだろう、私はこのアプローチを示唆していますにもかかわらず。ユーザーがiPhone 4のような小型の電話を使用していて、一部のセルが画面に表示されない場合はどうなりますか?その場合、tableView.cellForRowは非表示のセルに対してnilを返します。

私は、各セルにtableViewのviewControllerへの代理人を持つテキストフィールドを実装することをお勧めします(textField:shouldChange)。セル内でテキストが変更されると、代理人はその変更をインスタンス変数に保存するviewControllerに伝播する必要があります。

次に、ボタンを押すと、インスタンス変数から値を取るだけです。

0

あなたのtableViewCellは動的ではないと思います。 セルメソッドは次のようになります。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "YourCellID") as! YourClass 
    cell.yourTextField.tag = indexPath.row 
// 
// Alias will be tag 0 
// Primary Phone will be tag 1 
// Secondary Phone will be tag 2 
// Email Address will be tag 3, so on and so forth 
// Then, attach to each delegate 
// 
    cell.yourTextField.delegate = self 
} 

あなたのUITableView処理クラス(UITextFieldDelegateプロトコルに準拠)の中に、これを書いてください。

func textField(_ textField: UITextField, shouldChangeCharactersIn range:NSRange, replacementString string: String) -> Bool { 
let kActualText = (textField.text ?? "") + string 
switch textField.tag 
{ 
case 0: 
    aliasValueHolder = kActualText; 
case 1: 
    primaryPhoneValueHolder = kActualText; 
case 2: 
    secondaryPhoneValueHolder = kActualText; 
case 3: 
    emailAddressValueHolder = kActualText; 
default: 
    print("It is nothing"); 
} 
return true; 
} 

これを送信できます。

+0

多くの言語に反して、フォールスルーではないので、Swiftのswitchで 'break'の必要はありません。 – Moritz

+0

@EricAya、私はSwiftから来ていないし、事件の後に 'break'を追加しても何も悪いことはしません。それはまだ良いプログラミングの練習です。スウィフトは、実行文なしで大文字と小文字を残したり、デフォルトを残したりすると、まだ文句を言う –

7

私はついにこの簡単な解決策を考え出しました。

@IBAction func saveBtnAction(_ sender: Any) { 

    let index = IndexPath(row: 0, section: 0) 
    let cell: AddFriendC1Cell = self.myTableView.cellForRow(at: index) as! AddFriendC1Cell 
    self.alias = cell.aliasTextField.text! 
    self.primaryPhone = cell.primaryPhoneTextField.text! 
    self.secondaryPhone = cell.seondaryPhoneTextField.text! 
    self.email = cell.emailAddressTextField.text! 

    print("Alias: \(self.alias), Phone: \(self.primaryPhone), Phone2: \(self.secondaryPhone), Email: \(self.email)") 
} 
+0

すべてのテキストフィールドが最初のテーブルのセルセクションと行(行:0、セクション:0)にあり、他のセクションもありますが、スクロールしない限り表示されません。 –

関連する問題