I am using Custom cell(friendListCell) in my app. when i run this into an IOS 8 and later it works but in IOS 7 it crash and show error like below. i could not find the error where it present,
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UILabel 0x17892a10> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key keyPath.'
Below the code,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
return cell;
}
Note:
I delete all the reference to this cell even though it shows error.
That error message means that you are setting a variable on a UILabel from inside the storyboard/xib file that doesn't exist.
You can change the code anyway you want and that won't fix the problem. The error is in your storyboard or xib file. Are you trying to set a variable called keyPath in your storyboard/xib file?
Try this one for creating Cell:
static NSString *CellIdentifier = #"cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
Related
Terminating app due to uncaught exception
NSUnknownKeyException', reason: '[<__NSCFString 0x7f8392695300> valueForUndefinedKey:]: this class is not key value coding-compliant for the key name.
getting an exception.. i am trying to display database values to tableview
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Dequeue the cell.
//static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"idCellRecord" forIndexPath:indexPath];
NSInteger indexOfFirstname = [self.dbManager.arrColumnNames indexOfObject:#"name"];
NSInteger indexOfLastname = [self.dbManager.arrColumnNames indexOfObject:#"fname"];
cell.textLabel.text = [NSString stringWithFormat:# "%# %#", [[self.arrPeopleInfo objectAtIndex:indexPath.row]valueForKey:#"name"], [[self.arrPeopleInfo objectAtIndex:indexPath.row] valueForKey:#"fname"]];
[self.arrPeopleInfo objectAtIndex:indexPath.row]
Is returning a string, you are then attempting to call 'valueForKey:' on that and strings do not respond to that selector. Check that the contents of arrPeopleInfo are actually dictionaries.
Follow below steps:
1) Go to class xib file or if its in storyboard
2) Right click on UITableView, remove all earlier bindings
3) Add new binding by providing IBOutlet, delegate and datasource.
4) Clean project and run again.
The error means that your Table View isn't key-value compliant, meaning that KVP's (Key-Value Pairs) are unavailable for that type of object.
Check your storyboard n clear the warnings. There should be some connection errors or warnings
in your case there will be some yellow marks, clear it n see. it will work
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
I'm trying to display a TableView of a list of songs in a user's library. I used the code from this tutorial (which uses a storyboard, but I would like to try it a different way and with just a subclass of UITableView).
I get the error:
*** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-2903.23/UITableView.m:5261
2014-05-07 20:28:55.722 Music App[9629:60b] *** 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'
and an error Thread 1: SIGABRT on the line:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
This is my code:
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
MPMediaQuery *songsQuery = [MPMediaQuery songsQuery];
NSArray *songs = [songsQuery items];
return [songs count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
MPMediaQuery *songsQuery = [MPMediaQuery songsQuery];
NSArray *songs = [songsQuery items];
MPMediaItem *rowItem = [songs objectAtIndex:indexPath.row];
cell.textLabel.text = [rowItem valueForProperty:MPMediaItemPropertyTitle];
cell.detailTextLabel.text = [rowItem valueForProperty:MPMediaItemPropertyArtist];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Table-view-background.png"]];
cell.textLabel.textColor = [UIColor colorWithRed:0.278 green:0.278 blue:0.278 alpha:1.0];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Table-view-selected-background.png"]];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
return cell;
}
The app loads and works fine, showing a blank table when I run it in the iPhone simulator on my Mac. it comes up with this error when I run it on my iPhone.
Any help would be much appreciated, thanks!
If you create the table view programmatically, and you're just using the default UITableViewCell, then you should register the class (in viewDidLoad is a good place). You can also do this for a custom class, but only if you create the cell (and its subviews) in code (use registerNib:forCellWithReuseIdentifier: if the cell is made in a xib file).
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"];
However, this will only give you a "Basic" table view cell with no detailTextLabel. To get that type of cell, you should use the shorter dequeue method, dequeueReusableCellWithIdentifier:, which doesn't throw an exception if it doesn't find a cell with that identifier, and then have an if (cell == nil) clause to create the cell,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
//Configure cell
return cell;
}
Please, if you're using a custom class for your cell check that you've already registered the nib for the cell to use in your table view, as follow:
[self.yourTableViewName registerNib:[UINib nibWithNibName:#"YourNibName" bundle:nil]
forCellWithReuseIdentifier:#"YourIdentifierForCell"];
Apart from that please check that your custom UITableViewCell subclass has the appropiate reuse identifier.
If you're not using a custom class, please follow this instructions from Apple Docs:
The data source, in its implementation of the tableView:cellForRowAtIndexPath: method, returns a configured cell object that the table view can use to draw a row. For performance reasons, the data source tries to reuse cells as much as possible. It first asks the table view for a specific reusable cell object by sending it a dequeueReusableCellWithIdentifier:
For complete information, please go to this link: Creating a Table View Programmatically.
I hope this helps!
The reason for the crash is that you are using
[tableView dequeueReusableCellWithIdentifier: forIndexPath:]
which will cause a crash unless you specify a cell or nib file to be used for the cell.
To avoid the crash, use the following line of code instead
[tableView dequeueReusableCellWithIdentifier:];
This line of code will return nil if there is not a cell that can be used.
You can see the difference within the docs which can be found here.
There is also a good answer to a Q&A that can be found here
I know this is a bit late to answer this question but here is what I was missing.
I was doing:
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
But I should be doing this:
let cell = self.tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
Notice: the self. added next to tableView.
I hope this saves you 24 hours.
if you use default UITableViewCell (not customize one) [tableView dequeueReusableCellWithIdentifier: forIndexPath:], you must registerClass or registerNib; (dequeReusableCellIdentifier:forIndexPath won’t return nil cell, if the cell is not there, then it will automately create a new tableViewCell with the default UITableViewCellStyelDefault!)
otherwise, you can use [tableView dequeueReusableCellWithIdentifier] without registerClass or registerNib
If you are using StoryBoard then select that tableviewcontroller->table->Cell, give that Cell a 'Identifier' in attribute inspector.
Make sure you use the same 'Identifier' in 'cellforrowatindexpath' method.
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]];
}
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