我有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 回答
我想你的tableView只有一个部分 . 然后你可以这样做
step-1
在当前类中添加委托
UITextFieldDelegate
step-2
在
cellForRowAtIndexPath
上调用当前字段的委托并添加标记Step-3
拨打
UITextField Delegates
Step-4
如果
emailAddress & password
不包含通过错误的值,则称为使用您的函数