37

my work is about `UITableView. Each time I run my project, this error appears :

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell1 - must register a nib or a class for the identifier or connect a prototype cell in a storyboard

I checked a hundred times my cell identifier in my storyboard and in my code are the same. Code (defaut code from UITableViewController) :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"Cell1";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...

    return cell;
}

Picture of Table View Cell properties : enter image description here

I created and implemented a subclass of UITableViewCell for my cell.

Any idea why this is not working ?

Any way (line of code) to know what is the identifier of a cell ?

Thanks

Edit : Screenshot of my interface builder.

IB

Edit 2 : Text of customCell.h

#import <UIKit/UIKit.h>

@interface customCell : UITableViewCell

@end

New error appears when I run the project :

[<choixActiviteViewController 0x7591ac0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Cell1.

choixActiviteViewController is a subclass of UITableViewController and is the custom class of Choix Activite View Controller.

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
GoldXApp
  • 2,397
  • 3
  • 17
  • 26
  • 1
    Please, attach some relevant code. A screenshot of your attribute inspector would be useful too. – ssantos Sep 29 '13 at 23:07
  • 4
    How are you instantiating your view controller? If it isn't being instantiated from the storyboard via a segue or `[UIStoryboard instantiateViewControllerWithIdentifier:]`, your storyboard prototypes won't be registered. – Timothy Moose Sep 29 '13 at 23:14
  • This `UITableViewController`is my first view. There is only a navigation controller before with a relationship 'rootviewcontroller'. – GoldXApp Sep 29 '13 at 23:15
  • Did you checked that tableView is properly linked to the controller? – ssantos Sep 29 '13 at 23:17
  • I'm not sure how to check if the link is OK. I can ctrl+drag my `UITableView` and my `UITableViewCell`in my controller header file. Is it sufficient ? – GoldXApp Sep 29 '13 at 23:22
  • Just select the table in your storyboard, go to connections inspector, and make sure datasource and delegate are linked to the controller (if not, drag them to it) and there's a referencing outlet like _tableView – ssantos Sep 29 '13 at 23:24
  • Could you post a screenshot of the full interface builder screen? – ssantos Sep 29 '13 at 23:33
  • Yep, tableView is properly linked to the controller. – GoldXApp Sep 29 '13 at 23:37
  • Your code is all about the middle one view in the storyboard, right? – Oleg Sobolev Mar 02 '14 at 05:58
  • 1
    @TimothyMoose.. you sir, are a hero tonight.. i absentmindedly fired up my test tableViewController via alloc/init and spent an hour trying to figure out why my prototype was being ignored.. thank YOU.. – drew.. Nov 05 '15 at 05:18

11 Answers11

80

Instead of:

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

Try:

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if this does not work then, also add:

  if (cell == nil) {
    cell = [[customCell alloc] init];
}
penduDev
  • 4,743
  • 35
  • 37
Abdullah Shafique
  • 6,878
  • 8
  • 35
  • 70
25

Ok, your problem is that you're using Static cells, instead of Prototype cells. Just change your UITableView Content type.

Prototype cells option

ssantos
  • 16,001
  • 7
  • 50
  • 70
14

Ok, my project finally works. Some errors appeared about things I've deleted and I can't find anymore.

I've just deleted every TableViewCell I had to create a new unique UITableViewCell with the following properties :

class : customCell

Style : Basic (but it works also with Custom)

Identifier : Cell1

Thanks for your help ssantos, Abdullah Shafique and bilobatum.

GoldXApp
  • 2,397
  • 3
  • 17
  • 26
2

If your table cell is a subclass of UITableViewCell, then you're not using the "Basic" style of cell. Change the style setting in the attributes inspector to Custom.

Also, make sure the Class in the table cell's Identity inspector is set to your custom table cell subclass.

bilobatum
  • 8,918
  • 6
  • 36
  • 50
  • I have changed to "Basic style" to edit the "Title" in the TableViewCell. I tried with "Custom" type but same error again. – GoldXApp Sep 29 '13 at 23:28
  • From your screenshot, it doesn't look like your cell prototypes are subclasses of UITableView cell. So you're not subclassing UITableViewCell? – bilobatum Sep 29 '13 at 23:38
  • Yes, I think I do. In my IB, each cell has "customCell" in their custom class (in Identity inspector). In customCell.h : @interface customCell : UITableViewCell – GoldXApp Sep 29 '13 at 23:45
  • Here's an idea for troubleshooting: see if you can get your table cells to load without your prototypes subclassing UITableViewCell. – bilobatum Sep 30 '13 at 00:09
2

I suppose you'r using static cells. If you'r doing that consciously - just delete thous methods from your .m file:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
}
serg_ov
  • 563
  • 7
  • 18
1

If you are intentionally using Static UITableViewCells, then you don't have to implement the normal UITableView population methods (numberOfSectionsInTableView:, tableView:numberOfRowsInSection:, tableView:cellForRowAtIndexPath:).
The UITableView will automatically populate from the cells defined in your storyboard. You do end up using the tableView:cellForRowAtIndexPath: in order to format the data in the cells. You use [super tableView:cellForRowAtIndexPath:] to get the tableViewCell and then ReuseIdentifier, tags, or indexPath to match the cell to the data that goes with it.

Code cracker
  • 3,105
  • 6
  • 37
  • 67
Prof Von Lemongargle
  • 3,658
  • 31
  • 29
1

try to use customCell class instead UITableViewCell class.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell1";
customCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

// Configure the cell...

return cell;
}
Oleg Sobolev
  • 3,286
  • 2
  • 16
  • 29
1

The reason for this issue is very clear from the exception:

One Solution to this problem is register a class for the identifier

In Swift this can be done as follows

tableView.registerClass(UITableViewCell.classForKeyedArchiver(), forCellReuseIdentifier: "your_reuse_identifier")
arango_86
  • 4,236
  • 4
  • 40
  • 46
0

In my case the Storyboard where I had defined the missing UITableViewCell was localised.

Fran Pugl
  • 464
  • 5
  • 6
-1

Comment out all of the code in application:didFinishLaunchingWithOptions(AppDelegate) but remain return YES, and try it again.

realid
  • 1
-1

I was facing this issue when presenting the UITableViewController having a custom cell from another viewcontroller. As a workaround I instantiated it from the storyboard and that solved the issue.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "YourTableViewController")
self.present(controller, animated: true, completion: nil)
HRY
  • 1