繁体   English   中英

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

[英]Custom Table Cell TextField: no index path for table cell being reused Swift Xcode6 Beta 6

从自定义表格单元格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
}

如果您用该标识符注册了笔尖,则dequeueReusableCellWithIdentifier保证返回一个单元格。 出队后,您无需手动执行loadNibNamed 您要使一个单元出队,为其分配一个var然后手动创建一个新的单元并覆盖已出队的单元。 这就是为什么您不重复使用单元格的原因。

附带说明一下,您的类( mailTableCellpasswordTableCell ,...)应大写,因为它们是类,而不是对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM