自定义表格单元格TextField:没有索引路径表格单元格被重用Swift Xcode6 Beta 6

时间:2014-08-20 03:28:04

标签: ios xcode uitableview swift

当第一次从自定义表格单元格Nib使用文本字段显示时,我收到“没有表格单元格被重用的索引路径”,并且没有任何Textfield委托方法可以工作但是一旦退出textField并再次输入它,错误不显示,委托方法有效。

的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
}

1 个答案:

答案 0 :(得分:0)

如果您已使用该标识符注册了一个笔尖,则

dequeueReusableCellWithIdentifier可以保证返回一个单元格。出局后,您不需要手动执行loadNibNamed。您正在将单元格出列,为其分配var,然后手动创建新单元格并覆盖出列单元格。这就是为什么你不重复使用细胞的原因。

在旁注中,您的班级(mailTableCellpasswordTableCell,...)应该大写,因为它们是类,而不是对象。