我有一个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