When displaying using textfields for the first time from a custom table cell Nib, i receive "no index path for table cell being reused" and none of the Textfield delegate methods work but once exiting the textField and entering it again, the error does not display and the delegate methods work.
cellForRowAtIndexPath:
if indexPath.row == 0 {
var mailCell: mailTableCell = tableView!.dequeueReusableCellWithIdentifier("mailCell", forIndexPath: indexPath) as mailTableCell
var nib: NSArray = NSBundle.mainBundle().loadNibNamed("mailTableCell", owner: nil, options: nil)
mailCell = nib.objectAtIndex(0) as mailTableCell
mailCell.textField.delegate = self
mailCell.textField.tag = 1
mailCell.separatorInset = UIEdgeInsetsZero
return mailCell
} else if indexPath.row == 1{
var passCell = tableView!.dequeueReusableCellWithIdentifier("passCell", forIndexPath: indexPath) as passwordTableCell
var nib: NSArray = NSBundle.mainBundle().loadNibNamed("passwordTableCell", owner: nil, options: nil)
passCell = nib.objectAtIndex(0) as passwordTableCell
passCell.textField.delegate = self
passCell.textField.tag = 2
passCell.textField.returnKeyType = UIReturnKeyType.Go
passCell.separatorInset = UIEdgeInsetsZero
return passCell
} else {
var buttonCell = tableView!.dequeueReusableCellWithIdentifier("loginCell", forIndexPath: indexPath) as buttonTableCell
var nib: NSArray = NSBundle.mainBundle().loadNibNamed("buttonTableCell", owner: nil, options: nil)
buttonCell = nib.objectAtIndex(0) as buttonTableCell
buttonCell.separatorInset = UIEdgeInsetsZero
buttonCell.label.tag = 3
buttonCell.indicator.tag = 4
return buttonCell
}
viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
var mailCell = UINib(nibName: "mailTableCell", bundle: nil)
var passCell = UINib(nibName: "passwordTableCell", bundle: nil)
var loginCell = UINib(nibName: "buttonTableCell", bundle: nil)
tableView!.registerNib(mailCell, forCellReuseIdentifier: "mailCell")
tableView!.registerNib(passCell, forCellReuseIdentifier: "passCell")
tableView!.registerNib(loginCell, forCellReuseIdentifier: "loginCell")
keyboardHidden = true
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidHide:", name: UIKeyboardWillHideNotification, object: nil)
}
textFieldShoudBeginEditing:
func textFieldShouldBeginEditing(textField: UITextField!) -> Bool {
let mailField = self.tableView.viewWithTag(1) as UITextField
let passField = self.tableView.viewWithTag(2) as UITextField
if textField == mailField {
dispatch_async(dispatch_get_main_queue(), {
let mailCell = textField.superview?.superview as mailTableCell
UIView.transitionWithView(mailCell.icon, duration: 0.5, options: UIViewAnimationOptions.TransitionFlipFromRight, animations: {
mailCell.icon.highlighted = true
}, completion: nil)
})
}
if textField == passField {
dispatch_async(dispatch_get_main_queue(), {
let passCell = textField.superview?.superview as passwordTableCell
UIView.transitionWithView(passCell.icon, duration: 0.5, options: UIViewAnimationOptions.TransitionFlipFromRight, animations: {
passCell.icon.highlighted = true
}, completion: nil)
})
}
return true
}
dequeueReusableCellWithIdentifier
guarantees to return a cell if you have registered a nib with that identifier. You don't need to manually do loadNibNamed
after dequeuing. You are dequeuing a cell, assigning it a var
then manually creating a new cell and overwriting the dequeued cell. That's why you're not reusing the cells.
On a side note, your classes ( mailTableCell
, passwordTableCell
, ...) should be capitalized because they are classes, not objects.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.