2016-08-04 62 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 
    } 
} 
相关问题