4

I have a static table with one static section. Other sections are dynamic.

I create Table Section and Table Cell for dynamic section. Set identifier for Cell, set custom class for it and even do:

self.tableView.registerClass(UncheckedStoreTableViewCell.self, forCellReuseIdentifier: "StoreCell")

enter image description here

if i don't register it with code, then i get:

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

So when i use this:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if indexPath.section == 0 {
        return super.tableView(tableView, cellForRowAtIndexPath: indexPath)
    }

    let cell = tableView.dequeueReusableCellWithIdentifier("StoreCell", forIndexPath: indexPath) as! UncheckedStoreTableViewCell

    return cell
}

It works. But if i'm trying to change label: cell.myLabel.text = "one" or just print(cell.myLabel) got

BAD_INSTRUCTION

Arti
  • 7,356
  • 12
  • 57
  • 122

2 Answers2

2

You do not need to register your cell in code.

You have correctly set the identifier of the cell, however this is not enough. In addition to this you also need to open Identity Inspector for your cell and set the class of the cell to be UncheckedStoreTableViewCell. Here is an image showing you where you should set it:

enter image description here

Without this step Xcode will not be able to correctly associate your cell identifier with your custom cell as it doesn't know anything about it!

Andriy Gordiychuk
  • 6,163
  • 1
  • 24
  • 59
  • This is impossible. Make sure that you typed class name correctly. Can you upload your project somewhere? From the symptoms which you described the cell gets dequeued just fine - the problem is that its class is UITableViewCell and not your custom class. – Andriy Gordiychuk Feb 24 '16 at 16:57
  • Only helps when i change my `tableView` content from static to Dynamic Prototypes. Then all work correctly – Arti Feb 24 '16 at 17:10
  • 1
    For static table views you should not be implementing cellForRowAtIndexPath as well as other uitableviewdatasource methods. – Andriy Gordiychuk Feb 24 '16 at 17:15
  • This actually helped me. Somehow Xcode did not register my class when I filled it in manually before I made the class. Even though I had the name filled correct I had to refill the class name again. – nullforlife Jan 03 '17 at 11:49
2
  1. You can definitely use dynamic cells in a static table view.
  2. Don't expect a static table view to register your cell's identifier for you. Just do it yourself.
  3. Do you have outlets in the cell class to some view in interface builder? If I were you I wouldn't expect the table view to know about that. It will instantiate your cell class, and that's it. No outlets will be set. I think this is related: load nib in view subclass

By the way, if you've defined a custom .nib for your cell, there's this method: registerNib(_:forCellReuseIdentifier:)

Community
  • 1
  • 1
Andreas
  • 2,665
  • 2
  • 29
  • 38