首页 文章

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

提问于
浏览
2

我有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 回答

  • 5

    我想你的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)
            }
        }
    
  • 1

    step-1

    在当前类中添加委托 UITextFieldDelegate

    class ViewController: UIViewController,UITextFieldDelegate
    

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

    Step-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 = ""
      }
    }
    

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

相关问题