I have a program in which there is a tableViewController which has 1 section and 3 rows. Whenever I run the app, it always crashes at dequeueReusableCellWithIdentifier: method. I inserted a breakpoint and NSLog() to zero in the this method. I also set the Cell Identifier of the prototype cell in storyboard to Cell. But the program still crashes. Here's an method that is causing the problem:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"indexPath.section = %d, indexPath.row = %d", indexPath.section, indexPath.row);
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
NSLog(#"in nil");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
return cell;
}
The crash log reads:
MyAppTake4[7463:707] indexPath.section = 0, indexPath.row = 0
2012-11-05 23:21:20.747 MyCardAppTake4[7463:707] -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:]: unrecognized selector sent to instance 0xfbc9000
2012-11-05 23:21:20.753 MyCardAppTake4[7463:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableView dequeueReusableCellWithIdentifier:forIndexPath:]: unrecognized selector sent to instance 0xfbc9000'
*** First throw call stack:
(0x3254788f 0x3459d259 0x3254aa9b 0x32549915 0x324a4650 0x7e2f7 0x31fceefb 0x31fcdfd9 0x31fcd763 0x31f71f15 0x324a61fb 0x3420aaa5 0x3420a6bd 0x3420e843 0x3420e57f 0x342064b9 0x3251bb1b 0x32519d57 0x3251a0b1 0x3249d4a5 0x3249d36d 0x315f4439 0x31f9ccd5 0x7d9f9 0x7d994)
terminate called throwing an exception(lldb)
I am interested in identifying the exact cause of error so that I do not make this mistake again.
Thanks for your help.
Thats because [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; was added in iOS 6 but not recognized by iOS 5. For iOS 5 compatibility use below method instead:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Did you register the class of the cell or the nib first?
From the Apple docs:
Important: You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method.
(btw that method is only available in iOS 6)
If you're using iOS 6, then you can use the new simpler paradigm:
In viewDidLoad, register the class, in your case, that's just the UITableViewCell class:
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"];
Then, you can just do it like this in cellForRowAtIndexPath:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = self.myArray[indexPath.row];
return result;
}
Since forIndexPath method is for ios6, I recommend you to use this snipet. Find below.
UITableViewCell *cell = [table_view dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
// **initialize** your stuff here **with their respected tag**
}
else
{
// **get** your stuff back using their tags
}
// **assign** your valiues here
return cell;
Enjoy Programming
I had this exception, and realised that it was caused by my stupid mistake. I had created a blank .xib file (to have a custom "Loading" cell in my UITableView), but had then drag'n'dropped a View into it. Doh!
The result was the exception message which you're seeing, plus this in the Output window:
2014-04-25 20:45:12.953 Languages[36256:60b] *** Terminating app due to uncaught
exception 'NSInternalInconsistencyException', reason: 'invalid nib registered
for identifier (LoadingCell) - nib must contain exactly one top level object
which must be a UITableViewCell instance'
The solution, of course, was to add a Table View Cell to my .xib file, copy the other UI components into it, and just leave that Table View Cell as the "root" element in the file.
Another cause for this error can be-
invalid nib registered for identifier (Cell) - nib must contain exactly one top level object which must be a UICollectionReusableView instance
I had a UIView in my xib instead of a collectionViewCell. Hope this helps.
Related
I am trying to follow this tutorial but I am getting empty table.
http://www.techotopia.com/index.php/Using_Xcode_5_Storyboards_to_Build_Dynamic_TableViews_with_Prototype_Table_View_Cells
First, tutorial is using
{
CarTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
}
with this I have encountered an error:
2013-11-12 11:29:35.940 TableViewStory[14940:70b] *** Terminating app due to uncaught
exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with
identifier carTableCell - must register a nib or a class for the identifier or connect a
prototype cell in a storyboard'
So I added:
[self.tableView registerClass:[CarTableViewCell class]
forCellReuseIdentifier:#"carTableCell"];
on my viewDidLoad. After adding the registerClass I was able to build my application. However I am getting empty table.
Thank you.
You need to implement cellForRowAtIndexPath method appropriately:
- (UITableViewCell *)tableView:(UITableView *)iTableView cellForRowAtIndexPath:(NSIndexPath *)iIndexPath
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [iTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
// Customize your cell
return cell;
}
You didn't register a nib or a class for the reuse identifier CellIdentifier. You are using below piece
CarTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
Replace this with
CarTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
I re-did the tutorial and everything worked.
Thanks anyway.
I have been following a tutorial
It uses calls to change to table view which I do not want to use and have not added this but when it comes to this point
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//if (cell == nil) cell = [[PeerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSString *peerID = [_matchmakingServer peerIDForConnectedClientAtIndex:indexPath.row];
cell.textLabel.text = [_matchmakingServer displayNameForPeerID:peerID];
return cell;
}
I blanked out the if statement as this is where it will set the format for the data and now when you return cell it throws up an exception
Anyone got any ideas why?
Error Message...
Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /SourceCache/UIKit/UIKit-2380.17/UITableView.m:5471 2013-05-29
21:11:28.967 Iphone_controller[2485:907] * Terminating app due to
uncaught exception 'NSInternalInconsistencyException', reason:
'UITableView dataSource must return a cell from
tableView:cellForRowAtIndexPath:'
I guess it throws an exception because you're returning nil. Because you commented out the line where the cell would have been created... Not sure why you did that.
Technically, your code could work, but only if you register a NIB with the table view for your CellIdentifier before you try to use the table view.
Creating the cell if one can't be dequeued is the common and correct thing to do if you aren't registering the NIB.
Try:
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
Probably my code crashes in the following method, since the getData() is called only once - at the end of this method.
Moreover the didSelectRowAtIndexPath code is the following:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath
{
[tableView deselectRowAtIndexPath:newIndexPath animated:YES];
// Set
[[GlobalObject obj] setData:[(CustomCell*)[tableView cellForRowAtIndexPath:newIndexPath] getData]];
}
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CustomCell getData]: unrecognized selector sent to instance.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0)
{
...
return cell;
}
else
{
// Create a button table view cell
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:#"CustomCell"];
if (cell == nil)
{
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CustomCell"] autorelease];
}
[cell setButton:[ ...];
return cell;
}
}
As shown above the cell is autoreleased and probably this is the reason the app crashed. Cell went to the autorelease pool and then getData sent to an unrecognized selector. How can I resolve the issue?
CustomCell does noth have a method called getData.
Avoid inlining, write the statements one after another. This helps in debugging. You easily can then set a breakpoint.
I'm trying to make a custom cell using storyboard.
I have tested my program with Basic cells and it worked.
Now I'have created a new class that I named NewsCell, which is containing the different Labels in the custom cell.
I have also made the cell a subclass of NewsCell. The cell identifier is "NewsCell".
This is the cellForRowAtIndexPath: method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NewsCell *cell = (NewsCell *)[tableView dequeueReusableCellWithIdentifier:#"NewsCell"]; //signal : SIGABRT
News *info = [self.news objectAtIndex:indexPath.row];
cell.titreLabel.text = info.titre;
cell.descriptionLabel.text = info.description;
return cell;
}
When I run my app, it crashes with a signal signal SIGABRT at the first ligne.
I'm sure that I've made the right connections between Labels and the table view cell.
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'The NIB data is invalid.'
I had the same problem. It turned out that I had checked "Use Autolayout" in the Interface Builder Document pane of the Storyboard file. This throws the "invalid NIB" error when running on iOS 5 because autolayout is only supported on iOS 6.
You have not created instance for the cell. Try to create instance for the cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NewsCell *cell = (NewsCell *)[tableView dequeueReusableCellWithIdentifier:#"NewsCell"]; //signal : SIGABRT
if (cell == nil) {
cell = [[[NewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
News *info = [self.news objectAtIndex:indexPath.row];
cell.titreLabel.text = info.titre;
cell.descriptionLabel.text = info.description;
return cell;
}
This is either an XCode bug, or me missing a crucial rule here.
Update:
- What's the chance of this being a weird bug in XCode/Storyboard?
Situation:
iOS 5, Storyboard
This is the storyboard setup: http://i.imgur.com/T5WyD.png
Another screenshot of the full setup: http://i.imgur.com/1tVuz.png
TableViewController with Custom Cell, cell has reusable identifier "NewCell"
in "cellForRowAtIndexPath" I basically have:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"NewCell"];
return cell;
This throws an exception:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
Things I have already tried:
I setup a new project from scratch, TabBarController, with a TableViewController and a Custom Cell, all wired up in Storyboard, set a reusable cell identifier. Used the same code as above, worked perfectly. I don't know why it didn't work with the storyboard above...
I have a feeling it has something to do with the tree I built there, which is a TabBarController, loading a NavigationController, loading a TableViewController, providing a few items, one is clicked, which loads another TableViewController, which is unable to work with the custom cell, somehow.
Important:
- The issue is that the Storyboard should make sure that:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"NewCell"]; will never return NIL (unlike without the Storyboard/iOS4). But, mine is nil. And I can't, for the hell of it, figure out what's happening.
Heyo, I just had this problem today and figured out a few possible causes. To link a UITableView as a subview of a ViewController in Story board check that you have done these steps.
In your "ViewController.h", add <UITableViewDataSource> to your list
of protocols
#interface ViewController : UIViewController
<UITableViewDataSource>
you might want to add <UITableViewDelegate> as well but I didn't.
In your "ViewController.m" set up your Table view data source
functions
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [yourCells count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NewCell *cell = (NewCell *)[tableView
dequeueReusableCellWithIdentifier:#"NewCell"];
if (cell == nil) {
NSLog(#"Cell is NIL");
cell = [[CustomCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
return cell;
}
In Storyboard, connect the UITableView's "Data Source" and
"Delegate" references to the ViewController.
In Storyboard, give the Prototype Cell the Identifier "NewCell"
If these are done, I believe it should work.
Hope this helps =)
I always use custom cells in the storyboard like this:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
Make sure the number of rows/sections is at least one. And another thing to check is that you set the UITableViewController to your custom class name in the storyboard.
Did you double check the cell prototype identifier is "NewCell"?
Your setup as Tabbar->navigationcontroller->tableview is perfectly fine. I
am using this layout all the time.
verify also there's no useless runtime attributes anywhere in this
viewcontroller/tableview/cell.
your prototype looks a bit weird there - you added a UIButton to the cell as subview? why?
To solve this problem add the following just above your dequeueReusableCellWithIdentifier statement:
static NSString *CellIdentifier = #"NewCell";
making sure the identifier matches the prototype cell in Storyboard
It maybe late to answer this but i bet this will fix all your problems