我有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)
}
}
如何从单元格内部检索文本字段值以检查它们是否为空?上面的代码不起作用,因为文本字段始终返回空。虽然,希望可以看到我正在努力实现的目标
我猜你的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)
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句