I am getting the following error when I press a button that opens a tableview:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier title - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
Here is the code in the view controller for the tableview and the method that's causing the problem:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
I researched the error and I tried removing the forIndexPath:indexPath so the code looked like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return cell;
}
Now this caused a new error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:
now I did some logging to find out that cell == nil was true so I added a check for that as suggested by some earlier questions:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[UITableViewCell alloc] init]
}
return cell;
}
Now this removed all errors but now when I open the tableview, the cells are empty when I want the ones that I created in the storyboard.
How do I fix this problem?
Here is what the View Controller looks like in the storyboard:
there are two cell recycle methods you can call on a UITable
View,
-(UITableViewCell *) dequeueReusableCellWithIdentifier:forIndexPath:
-(UITableViewCell *)dequeueReusableCellWithIdentifier:
its somewhat confusing, but they are quite different in how they are used. The one which takes a second argument (of type NSIndexPath) is dependant on you first having registered a class or xib file with the tableView, in order that the tableView can create a cell ad-hoc for you when there isn't one handy for recycling. This first method will always return a cell, so you can code your cellForRowAtIndexPath: much like you have.
the second method (which takes only one argument, the (NSString *)cellIdentifier can and will return nil when there is no cell handy for recycling. So when you use this one you should test the result for nil and create a cell in that case.
eg
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellId = #"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
}
//etc etc decorate your cell...
return cell;
}
In order to exploit your cell prototypes you will need to register a class or xib for each row/section, so that the table knows which cell to create. The recycling stuff only really works once enough cells have been created to fill the screen and you start scrolling. Good luck
If you are creating your prototype cells in a Storyboard you need to set the "Identifier" field for them to your "CellIdentifier" string. You can do this by selecting the cell and looking in the Attributes Inspector.
If you are creating a separate .xib file for your UITableViewCell you need to call this method on your UITableView in code somewhere:
registerNib:forCellReuseIdentifier:
If you are doing everything in code and just using a UITableViewCell subclass that knows how to layout itself then you need to call this method on your UITableView:
registerClass:forCellReuseIdentifier:
Here is a link to the reference docs
Getting this error when I load my tableview and I'm not sure why:
Assertion failure in -[UITableView
_configureCellForDisplay:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UITableView.m:7344
I've added a UITableViewController in my storyboard and I'm using the prototype cells to design the cell. I linked the cell to my custom UITableViewCell subclass and I set its reuse identifier to "Cell". I am getting the assertion failure when I call dequeueReusableCellWithIdentifier in cellForRowAtIndexPath. From what I've read, this happens when I don't set the reuse identifier in the prototype cell, however I've checked countless times and it's the same identifier I'm using when I dequeue it.
Here are my datasource methods In the UITableViewController subclass:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
LanguageSelectionTableViewCell *cell = (LanguageSelectionTableViewCell*)[self.tableView dequeueReusableCellWithIdentifier:#"Cell"];
return cell;
}
Try to use
[self.tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
instead of dequeueReusableCellWithIdentifier
You have to register the tableviewcell with the given reusablecellidentifier "Cell".
Example code is given below.
[yourTableView registerNib:[UINib nibWithNibName:#"YouTableViewCellNibName" bundle:nil] forCellReuseIdentifier:#"CellIdentifier"];
I added a new TableView in my application. I changed the cell-type to static and dragged some labels into the cells. Now i want to access the cells programmatically. For example: Cell 4 in Section 3 should open safari with google.com.
I created a new UITableViewController-class. Then i changed the number of sections to 3, and added a switch/case statement to the the numberofcellsinsection method.
If i run the app and open up the table view, the app crashes. Can someone help me, with setting up the TableView ?
Thanks!
EDIT
Here is the log
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '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'
*** First throw call stack:
I found a solution which is working.
i edited the cellForRowAtIndexPath method like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
return cell;
}
Now i can access my static cells with the if statements for example:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 2){
if (indexPath.row == 1){
NSLog(#"Tap");
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"http://www.google.com"]];
}
}
}
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 :
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.
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.
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];
}
Ok, your problem is that you're using Static cells, instead of Prototype cells. Just change your UITableView Content type.
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.
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.
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
{
}
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;
}
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.
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")
In my case the Storyboard where I had defined the missing UITableViewCell was localised.
Comment out all of the code in application:didFinishLaunchingWithOptions(AppDelegate) but remain return YES, and try it again.
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)
I got exception on on dequeueReusableCellWithIdentifier:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"pointcell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];// Here is exception
if( cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
...
return cell;
}
Exception:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UITableViewCell 0xca51470> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key accessoryActionSegueTemplate.'
I did set identifier to Custom type cell in Storyboard. I have set Accessory Action segue in Storyboard. I think it's related to that particular type but I don't understand what should I do prevent crash.
It seems a bit too late, but I've just faced the same problem and found a solution.
When you Control+Dragging a segue from UITableViewCell in Interface Builder it offers you to create a segue for cell or accessory view. If you create it for accessory view and run your app under iOS 5 you are in trouble since accessory segues were not supported in that version.
You can drag a segue from ViewController instead and include this code (taken from here):
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"SegueID"
sender:[tableView cellForRowAtIndexPath:indexPath]];
}