2016-08-04 58 views
2

我有一個tableview和2個單元格。兩個單元格都有一個文本框和一個標籤。 Textfield取用戶名/電子郵件地址和密碼的值。這裏下面如何從tableview單元格中檢索textfield值

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

} 

如何檢索單元格內的文本字段值以檢查它們是否爲空?上面的代碼不起作用,因爲文本字段總是返回空白。雖然,希望可以看到我想要到acheive

+1

加載tableview中時,上面的代碼執行。到那時textField是空的。您應該嘗試使用UITextFieldDelegate觀察textField在編輯或完成時的動態更改。 – ronan

回答

1

我猜你的tableView只有一個部分。那麼你可以做這樣

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步

附加委託在當前類UITextFieldDelegate

class ViewController: UIViewController,UITextFieldDelegate 

步驟2中

在你的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

呼叫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

呼叫作爲useual你的函數,如果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 - 坦克哥們 –

+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 
    } 
} 
相關問題