2016-08-04 6 views
2

2つのセルを持つテーブルビューがあります。両方のセルには、テキストフィールドとラベルがあります。テキストフィールドユーザー名/電子メールアドレスとパスワードの値を取得します。以下ここでテーブルビューのセルからテキストフィールドの値を取得する方法

var emailAddress: String? 
var password: String? 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier(cellID) as! TextFieldCell 

    cell.label.text = loginOptions[indexPath.row] 
    cell.textField.placeholder = loginOptions[indexPath.row] 

    if indexPath.row == 0 { 
     emailAddress = cell.textField.text! 
    } 
    if indexPath.row == 1 { 
     password = cell.textField.text! 
    } 

    return cell 
} 

テキストボックスに任意のテキストがあるかどうかをチェックし、そうでない場合は、すべてのフィールドに記入することを示唆している警告、コントローラを表示する機能です。

func signInButtonTapped() { 

    if emailAddress!.isEmpty || password!.isEmpty { 

     let alert = UIAlertController(title: "Alert", message: "all fields are required to be filled in", preferredStyle: .Alert) 
     let action = UIAlertAction(title: "OK", style: .Default, handler: nil) 
     alert.addAction(action) 
     self.presentViewController(alert, animated: true, completion: nil) 
    } 

} 

セル内からテキストフィールドの値を取得して空であるかどうかを確認するにはどうすればよいですか?上記のコードは、テキストフィールドが常に空になるため機能しません。が、うまくいけば、私があなたの現在のクラスにUITextFieldDelegate

class ViewController: UIViewController,UITextFieldDelegate 

ステップ-2

を代理人を追加

+1

テーブルビューがロードされたときに、上記のコードが実行を行うことができます。その時までにtextFieldは空です。 UITextFieldDelegateを使用して、編集または終了時にtextFieldの動的変化を観察する必要があります。 – ronan

答えて

1

あなたのtableViewにはセクションが1つしかないと思います。そして、あなたはこのよう

func signInButtonTapped() { 
     let indexpathForEmail = NSIndexPath(forRow: 0, inSection: 0) 
     let emailCell = tableView.cellForRowAtIndexPath(indexpathForEmail)! as TextFieldCell 

     let indexpathForPass = NSIndexPath(forRow: 1, inSection: 0) 
     let passCell = tableView.cellForRowAtIndexPath(indexpathForPass)! as TextFieldCell 

     if emailCell.textField.placeholder!.isEmpty || passCell.textField.placeholder!.isEmpty { 

      let alert = UIAlertController(title: "Alert", message: "all fields are required to be filled in", preferredStyle: .Alert) 
      let action = UIAlertAction(title: "OK", style: .Default, handler: nil) 
      alert.addAction(action) 
      self.presentViewController(alert, animated: true, completion: nil) 
     } 
    } 
+0

シンプルで完璧に動作します、ありがとう! – luke

4

ステップ-1

をacheiveしようとしているものを見ることができますcellForRowAtIndexPathの現行フィールドに代理人を呼び出してタグを追加してください。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier(cellID) as! TextFieldCell 

    cell.label.text = loginOptions[indexPath.row] 
    cell.textField.placeholder = loginOptions[indexPath.row] 
    // add the following lines 
    cell.textField.delegate = self 
    cell.textField.tag = indexPath.row 


    return cell 
} 

ステップ-3

useualとしてUITextField Delegates

func textFieldDidEndEditing(textField: UITextField) { 
print("TextField did end editing method called") 

if !textField.text.isEmpty // check textfield contains value or not 
{ 
    if textField.tag == 0 
    { 
    emailAddress = textField.text! 
    } 
    else 
    { 
    password = textField.text! 
    } 
} 

func textFieldShouldReturn(textField: UITextField) -> Bool { 

textField.resignFirstResponder(); 
return true; 
} 

    func textFieldDidBeginEditing(textField: UITextField!) {  
    if textField.tag == 0 
    { 
    emailAddress = "" 
    } 
    else 
    { 
    password = "" 
    } 
} 

ステップ-4

コールemailAddress & passwordもしあなたの関数に値が含まれていない呼び出して、エラーによる

func signInButtonTapped() { 

    if emailAddress!.isEmpty || password!.isEmpty { 

     let alert = UIAlertController(title: "Alert", message: "all fields are required to be filled in", preferredStyle: .Alert) 
     let action = UIAlertAction(title: "OK", style: .Default, handler: nil) 
     alert.addAction(action) 
     self.presentViewController(alert, animated: true, completion: nil) 
    } 
else 
    { 
    // continue your work 
    } 

} 
+0

同じ回答を投稿してください!完璧なソリューションupvoted! –

+0

@PranavGupte - tanx buddy –

+0

小さな問題があるようです。ユーザー名とパスワードに値を入力すると、値が設定されます。値を削除してスペースを空白のままにしてユーザー名/パスワードを設定すると、空のテキストフィールドではなく同じ値が設定されます。 – luke

0
func textFieldDidEndEditing(_ textField: UITextField) { 
    var tf = textField 
    repeat {tf = superView.tf!} while !(tf is UITableViewCell) 
    let cell = tf as! TextFieldCell 
    let indexPath = self.tableView.indexPath(for: cell) 

    //do further customization with indexPath 
    switch indexPath.row { 
     case 0: //assign value of row one to your data model, 
     case 1: //assign value of row two to your data model 
     default: break 
    } 
} 
関連する問題