簡體   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