简体   繁体   English

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

[英]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. 附带说明一下,您的类( mailTableCellpasswordTableCell ,...)应大写,因为它们是类,而不是对象。

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

相关问题 没有索引路径表格单元格在Swift中重用 - No index path for table cell being reused in Swift 断言没有重用表单元格的索引路径 - Assert no index path for table cell being reused 没有重用表单元格的索引路径 - No index path for table cell being reused iOS 6/7中“无法使用表格单元索引路径”消息的含义是什么? - What is the meaning of the “no index path for table cell being reused” message in iOS 6/7? 核心数据:没有重用表单元的索引路径,单元消失了 - Core Data: no index path for table cell being reused, Cells disappearing “表单元没有索引路径被重用”导致应用程序崩溃 - “no index path for table cell being reused” leading to app crash 自定义表格单元格不符合NilLiteralConvertible Swift Xcode 6 Beta 5 - Custom Table cell does not conform to NilLiteralConvertible Swift Xcode 6 Beta 5 在执行UItableView委托/数据源方法之前,控制台中显示“之前没有重用表格单元的索引路径”消息 - before executing UItableView delegate/datasource method “no index path for table cell being reused before” message is displayed in console PickerView没有显示任何值。 它发出警告,如表单元没有索引路径被重用? - PickerView doesn't show any value. It gives warning like no index path for table cell being reused? 无法访问表格视图单元的索引路径 - Index Path for the table view cell is not being accessible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM