I have an UITextField in one of the UITableViewCell rows. When I select the row, the delegate methods of the text field are getting called. But, when I tap on the UITextField directly, the app crashes with the following error,
fatal error: unexpectedly found nil while unwrapping an Optional value
The app crashes only when the UITextField is tapped. The tableView is loaded correctly with the given data.
This is the code,
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
cell.detailTextLabel?.hidden = true
let txtFld = UITextField()
cell.contentView.addSubview(txtFld)
txtFld.tintColor = appGlobals.appThemeColor
txtFld.delegate = self
txtFld.textColor = appGlobals.appThemeColor
txtFld.returnKeyType = UIReturnKeyType.Done
txtFld.keyboardType = UIKeyboardType.EmailAddress
txtFld.autocorrectionType = UITextAutocorrectionType.No
txtFld.autocapitalizationType = UITextAutocapitalizationType.None
txtFld.translatesAutoresizingMaskIntoConstraints = false
txtFld.text = self.appGlobals.getSelectedContactEmail()
txtFld.tag = 99
cell.addConstraint(NSLayoutConstraint(item: txtFld, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: cell.textLabel, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 15))
cell.addConstraint(NSLayoutConstraint(item: txtFld, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: cell.frame.size.width - 30))
cell.addConstraint(NSLayoutConstraint(item: txtFld, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 20))
cell.addConstraint(NSLayoutConstraint(item: txtFld, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: cell.textLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
cell.selectionStyle = UITableViewCellSelectionStyle.None
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let txtField = cell.viewWithTag(99) as! UITextField
txtField.becomeFirstResponder()
}
And the UITextField Delegate methods,
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
return true
}
func textFieldDidBeginEditing(textField: UITextField) {
NSLog("text field became first responder")
}
UPDATE1:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
cell.detailTextLabel?.hidden = true
if(cell.contentView.viewWithTag(99) == nil){
let txtFld = UITextField()
cell.contentView.addSubview(txtFld)
txtFld.tintColor = appGlobals.appThemeColor
txtFld.delegate = self
txtFld.textColor = appGlobals.appThemeColor
txtFld.returnKeyType = UIReturnKeyType.Done
txtFld.keyboardType = UIKeyboardType.EmailAddress
txtFld.autocorrectionType = UITextAutocorrectionType.No
txtFld.autocapitalizationType = UITextAutocapitalizationType.None
txtFld.text = self.appGlobals.getSelectedContactEmail()
txtFld.tag = 99
cell.selectionStyle = UITableViewCellSelectionStyle.None
return cell
}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let txtField = cell.contentView.viewWithTag(99) as? UITextField {
txtField.becomeFirstResponder()
}
}
UPDATE2:
This is the custom tableview cell's code,
import UIKit
class TxtFieldTableViewCell: UITableViewCell {
let appGlobals = AppGlobals()
let txtFld = UITextField(frame: CGRectMake(15, 27, AppGlobals().getScreenWidth() - 30, 20))
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
txtFld.tintColor = appGlobals.appThemeColor
txtFld.textColor = appGlobals.appThemeColor
txtFld.returnKeyType = UIReturnKeyType.Done
txtFld.autocorrectionType = UITextAutocorrectionType.No
txtFld.autocapitalizationType = UITextAutocapitalizationType.None
self.addSubview(txtFld)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
And I've used it in my code like this,
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell : TxtFieldTableViewCell? = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as? TxtFieldTableViewCell
if cell == nil {
cell = TxtFieldTableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
}
if(cell!.viewWithTag(99) == nil){
cell!.txtFld.delegate = self
cell!.txtFld.keyboardType = UIKeyboardType.EmailAddress
cell!.txtFld.text = self.appGlobals.getSelectedContactEmail()
cell!.txtFld.tag = 99
cell!.selectionStyle = UITableViewCellSelectionStyle.None
}
}
Still getting the same error.