如何从tableview单元格中检索文本字段值

时间:2016-08-04 09:12:06

标签: ios swift

我有2个单元格的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)
    }

}

如何从单元格内部检索文本字段值以检查它们是否为空?上面的代码不起作用,因为textfields总是返回空。虽然,希望可以看出我试图实现的目标

3 个答案:

答案 0 :(得分:5)

<强>步骤-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

如果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
  }

}

答案 1 :(得分: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)
        }
    }

答案 2 :(得分: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
    }
}
相关问题