我有一個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
加載tableview中時,上面的代碼執行。到那時textField是空的。您應該嘗試使用UITextFieldDelegate觀察textField在編輯或完成時的動態更改。 – ronan