unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard

前端 未结 22 2115
面向向阳花
面向向阳花 2020-12-12 17:33

I am fairly new to coding in general and really new to Xcode (Swift). I understand that I need to register a nib or a class but I don\'t understand \'where or how?\'.

<
相关标签:
22条回答
  • 2020-12-12 18:10

    Match the identifier name at both places

    This error occurs when the identifier name of the Tablecell is different in the Swift file and in the Storyboard.

    For example, the identifier is placecellIdentifier in my case.

    1) The Swift File

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cell = tableView.dequeueReusableCell(withIdentifier: "placecellIdentifier", for: indexPath)
        
        // Your code 
    
        return cell
    }
    

    ###2) The Storyboard

    0 讨论(0)
  • 2020-12-12 18:10

    In Swift 3.0, register a class for your UITableViewCell like this :

    tableView.register(UINib(nibName: "YourCellXibName", bundle: nil), forCellReuseIdentifier: "Cell")
    
    0 讨论(0)
  • 2020-12-12 18:10

    I had the same problem. This issue worked for me. In storyboard select your table view and change it from static cells into dynamic cells.

    0 讨论(0)
  • 2020-12-12 18:16

    Just for those new to iOS buddies (like me) who decided to have multiple cells and in a different xib file, the solution is not to have identifier but to do this:

    let cell = Bundle.main.loadNibNamed("newsDetails", owner: self, options: nil)?.first as! newsDetailsTableViewCell
    

    here newsDetails is xib file name.

    0 讨论(0)
  • 2020-12-12 18:16

    I was also struggling with the same problem. I had actually deleted the class and rebuilt it. Someone, the storyboard had dropped the link between prototype cell and the identifier.

    I deleted the identifier name and re-typed the identifier name again.

    It worked.

    0 讨论(0)
  • 2020-12-12 18:18

    You can register a class for your UITableViewCell like this:

    With Swift 3+:

    self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
    

    With Swift 2.2:

    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    

    Make sure same identifier "cell" is also copied at your storyboard's UITableViewCell.

    "self" is for getting the class use the class name followed by .self.

    0 讨论(0)
提交回复
热议问题