Custom UITableViewCell using Storyboard - ios5

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

Related

UISearchDisplayController and UITableView prototype cell crash

I have a UIViewController setup in a storyboard with a tableview and UISearchDisplayController.
I am trying to use a custom prototype cell from self.tableview (which is connected to main tableview in the storyboard). It works fine if self.tableview had returned at least 1 cell when I load my view, but if self.tableview doesn't load a cell (as there is no data), and I load up the UISearchBar and search, the cellForRowAtIndexPath: method crashes:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomSearchCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"CustomSearchCell" forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
-(void)configureCell:(CustomSearchCell *)cell atIndexPath:(NSIndexPath *)indexPath {
User *user = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.nameLabel.text = user.username;
}
Errors:
*** Assertion failure in -[UITableViewRowData rectForRow:inSection:heightCanBeGuessed:]
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect at invalid index path (<NSIndexPath: 0x9ef3d00> {length = 2, path = 0 - 0})
My fetchedResultsController seems to have data (1 section, 2 rows) at the point the above method is called. It crashes on the dequeueReusableCellWithIdentifier line.
Any pointers/ideas? It should dequeue the prototype cell from self.tableview, but my guess is there was none created in self.tableview so this is the cause?
UISearchDisplayController manages it's own UITableView (filtered table), in addition to having your primary table. The cell identifier in the filtered table doesn't match your primary table. You also want to fetch the cell not by indexPath as both tables can be vastly different from each other with respect to number of rows, etc.
So instead of doing this:
UITableViewCell *cell =
[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
instead do this:
UITableViewCell *cell =
[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
I solved this by duplicating the prototype cell into a new xib:
In viewDidLoad:
[self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:#"CustomSearchCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:#"CustomSearchCell"];
And updated cellForRowAtIndexPath to use the method's tableview not the original self.tableview:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CustomSearchCell" forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
If you use à UISearchDisplayController in the methode celForRowAtIndexPath, you must use the tableView parameter methode and not the retain pointer of the controller.
try with this code
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CustomSearchCell" forIndexPath:indexPath];

table view calling an error when passing data xcode

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

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.

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.

custom uitableViewCell in controller which inherit from UITableViewController

I want to create custom UiTableViewCell, and it works, when I create TableView, like in this example, by inherit from UIViewController. But when I create controller which inherit from UITableViewController (not UIViewController), and in xib create UITableViewCell, and of course hook up IBOutlet, i get this error:
*** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:5678
2011-05-06 07:21:34.107 tabela[11424:207] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { return cellOne; }
This is the sample reference
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// In your case it has to be the custom cell object here.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"AnIdentifierString"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"AnIdentifierString"] autorelease];
}
cell.textLabel.text = #"This text will appear in the cell";
return cell;
}