table view calling an error when passing data xcode - iphone

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];

Related

Using Xcode 5 Storyboards to Build Dynamic TableViews with Prototype Table View Cells

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.

Program crashes at dequeueReusableCellWithIdentifier:

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.

Custom UITableViewCell using Storyboard

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;
}

UITableViewCell from IB Shows Value as NULL

The following is being used as the code to put a tableviewcell from a nib file into the tableview. I have taken the tableviewcell in the same nib as that of the tableview. I have tried initializing the cell in the viewdidload also but to no go.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"identifier";
NSLog(#"the cellidentifier is %#", CellIdentifier);
bookmarksTableViewCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSLog(#"the cell is %#", bookmarksTableViewCell);
return bookmarksTableViewCell;
}
The error that I get is
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
I have checked connections in the IB and rewired them too but the result comes up the same. When I try NSLog'ing the cell, it comes as null.
You need to do it like so
static NSString *CellIdenetifier = #"Cell";
BookMarksTableViewCell *cell = (BookMarksTableViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[BookMarksTableViewCell alloc] initWithStyle:UITableViewCellStyleSubTitle reuseIdentifer:CellIdentifier];
}
Im not sure if you need that if but I thought I include it.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"identifier";
NSLog(#"the cellidentifier is %#", CellIdentifier);
bookmarksTableViewCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (bookmarksTableViewCell == nil){
//allocate your cell here
}
return bookmarksTableViewCell;
Of course it is null.
If a table view needs to display more cells than available in its queue, it returns nil from
dequeueReusableCellWithIdentifier:
to ask the data source for creating a new cell until the sufficient amount of cells are allocated. Check for NULL:
bookmarksTableViewCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (bookmarksTableViewCell == NULL)
bookmarksTableViewCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStylePlain reuseIdentifier:cellIdentifier] autorelease];
return cell;
Ah, ps.: forget about Interface Builder.

UITableViewCell is Nil

I'm sure this question has a simple enough answer, but I can't seem to find it. I have the following code in my UITableViewController:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil)
{
NSLog(#"Cell is nil!");
}
return cell;
}
But in my log output I get
Cell is nil!
and immediately afterwards
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
* First throw call stack:
(0x1b68d72 0x135fe51 0x1b68bd8 0xb0e315 0xcb373 0x63578 0xcb1d3 0xcff19 0xcffcf 0xb9384 0xc952e 0x691bc 0x13736be 0x215c3b6 0x2150748 0x215055c 0x20ce7c4 0x20cf92f 0x2171da2 0x1b4b4 0x1be63 0x2c2be 0x2cf9f 0x1f3fd 0x1ac5f39 0x1ac5c10 0x1adeda5 0x1adeb12 0x1b0fb46 0x1b0eed4 0x1b0edab 0x1b28f 0x1ce71 0x253d 0x2465)
libc++abi.dylib: terminate called throwing an exception
(lldb)
Does anyone have any idea why this is happening, and more importantly, how to fix it?
Thanks in advance.
You must create cell if it nil, because method
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return unused cell from table view cache.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:NSIndexPath*)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//configure cell
return cell;
}
dequeueReusableCellWithIdentifier returns an already created cell with the identifier you pass in, if one exists that can be reused.
If not, you're responsible for creating one.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:kCustomCellID] autorelease];
}
Yes, the methodUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];means it will reuse a cell which has been allocated with the CellIdentifier.but you have not allocated any cells with this identifier, for the tableView, it will allocate some cells for a screen, and then it will reuse the cells with the identifier, if you do like this, you will find that it will work
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil)
{
NSLog(#"Cell is nil!");
cell = [[[UITableViewCell alloc] initWithSyle:UITableViewCellStyleNormal reuseIdentifier:CellIdentifier] autorelease];
}
return cell;
}
take a test.
Do you use interface builder for this class? If so, don't forget to make a reference from the table view. I had the same nil issue, and this was the case for me.