簡體   English   中英

如何從TableView單元格檢索文本字段值

[英]how to retrieve textfield value from tableview cell

我有2個單元格的tableview。 兩個單元格都有一個文本字段和一個標簽。 文本字段采用用戶名/電子郵件地址和密碼的值。

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

}

如何從單元格內部檢索文本字段值以檢查它們是否為空? 上面的代碼不起作用,因為文本字段始終返回空。 雖然,希望可以看到我正在努力實現的目標

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

第四步

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

}

我猜你的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)
        }
    }
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
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM