[英]Custom Table Cell TextField: no index path for table cell being reused Swift Xcode6 Beta 6
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. 从自定义表格单元格Nib首次使用文本字段显示时,我收到“没有重用表格单元格的索引路径”,并且没有Textfield委托方法起作用,但是一旦退出textField并再次输入,则不会显示错误并且委托方法起作用。
cellForRowAtIndexPath: 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: 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: 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. 如果您用该标识符注册了笔尖,则
dequeueReusableCellWithIdentifier
保证返回一个单元格。 You don't need to manually do loadNibNamed
after dequeuing. 出队后,您无需手动执行
loadNibNamed
。 You are dequeuing a cell, assigning it a var
then manually creating a new cell and overwriting the dequeued cell. 您要使一个单元出队,为其分配一个
var
然后手动创建一个新的单元并覆盖已出队的单元。 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. 附带说明一下,您的类(
mailTableCell
, passwordTableCell
,...)应大写,因为它们是类,而不是对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.