Unable to deque cell with identifier title - ios

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

Related

Custom prototype cell in UIStoryboard is created but doesn't show up in UITableView

I have a project where I need to use a custom UITableViewCell. I'm designing the cell as a prototype in storyboard and it looks fine there. I assign the prototype to my custom UITableViewCell subclass, give it the same reuse identifier I'm using in my UITableView and link the UILabel on the prototype cell to an IBOutlet in my UITableViewCell subclass.
When I call it from the UITableView the cell is created and if I add labels and buttons in the code of that class (create them with a CGRect and all) they all work but the labels I've added in the storyboard never show up.
I don't understand how my subclass can be called and created successfully but its layout and subviews from the storyboard don't seem to exist as far as my app is concerned. What am I doing wrong?
Here's my cellForRowAtIndexPath code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
// Configure the cell...
return cell;
}
I've run into this issue before, and in my case, the problem was that the auto-generated code for the view controller included a call to:
[UITableView registerClass:forCellReuseIdentifier:]
I would suggest checking for and removing any calls to the above, or to
[UITableView registerNib:forCellReuseIdentifier:]
and trying your original code again.
acreichman, add casting in cellForRow and put an NSLog in you cell's awakeFromNib to see if you get there. Let me know...
Your cellForIndexViewPath should look like this, to create a simple custom cell with label,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
Make sure that you have made all connections well, set datasource and delegate to table and then set the “Identifier” of the custom cell to "MyTableViewCell" in “Attributes Inspector” like this,
For storyboard:
Add "MyTableViewCell" instead of "SimpleTableCell" as shown in above screenshot.

Xcode unable to dequeue a cell with identifier

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)

UITableCell subclass never released?

I'm using ARC but it seems that my custom UITableCellView is not release.
TBMListingLineView is a subclass of TBMGlobalCustomCell which is a subclass of UITableCellView.
In TBMListingLineView there are 10 UILabels (nonatomic, retain)
I've implemented in both classes the method dealloc which is never called (breakpoint doesn't stop the execution)
When I'm scrolling the TableView, the number of UILabel is increasing in Instruments/Allocations and that causes the application crashed after several memory warning.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = #"Cell";
TBMGlobalCustomCell* cell;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
switch(sortIndex) {
case 0 :
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil || ![cell isKindOfClass:[TBMListingLineView class]]) {
cell = [[TBMListingLineView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
break;
....
return cell;
}
The first problem is that you call dequeueReusableCellWithIdentifier twice for each cell.
And then you "throw away" the second dequeued cell also if it does not have the right class.
A better solution is to use different cell identifiers for each cell (sub)class used in
the table view, so that dequeueReusableCellWithIdentifier returns instances of the correct
class.

dequeueReusableCellWithIdentifier does not work

I want to load around 6000 - 8000 rows in a UITableview. I get the data from the server using a async call and when I get the data I call
[tableView reloadData]
This is to refresh the table view . But because of some reason my app gets stuck and freezes .
When I debug , I found that cellforrowatindexpath is called 6000 times (on main thread) and
dequeueReusableCellWithIdentifier always returns null .
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CDTableRowCell *cell = nil;
// Create and Resue Custom ViewCell
static NSString *CellIdentifier = #"CellIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// got into render/theme objec
if(cell == nil){
cell = [[CDTableRowCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// MODIFYING CELL PROPERTIES HERE FROM AN ARRAY
// NO HTTP CALLS
}
Also, tableview starts reusing cell once I start scrolling but before that I never always create a new one.
Any clue why this strange behavior ???
try like this,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier =#"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
return cell;
}
The method in your question is not a table view datasource method. The datasource method has the table view as an argument. The method you have written is one that can be used to obtain a cell from the tableView itself, not to obtain a new cell from the datasource.
I don't know how often that method is called but overriding it is almost certainly not what you want to do.
I'm guessing you have subclassed a uitableview to be its own datasource? If so, you need to have the code in your question in the datasource method tableView:cellForRowAtIndexPath:, and not override the method as you have now.

Custom UITableViewCells Alloc Problrm

So I have custom UITableViewCells (made with storyboard), and when I do not literally alloc, init the cells, the app crashes. However, since they are custom cells, I don't know how to alloc/init them.
This code crashes:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewCells *cell = (TableViewCells *)[tableView dequeueReusableCellWithIdentifier:#"TableViewCellsID"];
return cell;
}
with this printed to the console:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
I understand this means its crashing because there is no cell allocated yet, however when I try to allocate it, I have to specify a cell style.
So, when I build the cell as follows, it doesn't crash:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewCells *cell = (TableViewCells *)[tableView dequeueReusableCellWithIdentifier:#"TableViewCellsID"];
if (cell == nil)
cell = [[TableViewCells alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"TableViewCellsID"];
return cell;
}
The problem is the code that works (the second set), requires me to set a UITableViewStyle. And as far as I know, there is no UITableViewStyleCustom.
Any helps would be appreciated.
It crashes in first case because there is no cell to reuse, and you are calling dequeueReusableCellWithIdentifier.
EDIT -
For custom cell initialisation you can check following threads -
How to make custom TableViewCell with initWithStyle after 3.0
initWithFrame vs initWithStyle

Resources