-->

UITableViewCell error - this class is not key valu

2019-03-20 15:24发布

问题:

This question already has an answer here:

  • Class is not key value coding-compliant [duplicate] 8 answers

I'm getting the following error when I try to load a custom UITableViewCell from a xib file via UINib's instantiateWithOwner method. I've tried all of the other solutions I can find on here with no luck. The issue seems to be that when the xib file is opened up by UINib, it uses the super class UITableViewCell instead of my custom class, ContentPackCell. I have attached a screenshot from Interface Builder showing where I associated the xib with my class as well associating an Identifier. There has to be some other step that I'm missing.

The error:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UITableViewCell 0x6b87220> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key descriptionLabel.'

The code (similar to Apple's sample AdvancedTableViewCells project):

ContentPackCell *cell = (ContentPackCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    [self.cellNib instantiateWithOwner:self options:nil];
    cell = tmpCell;
    self.tmpCell = nil;
}


Update:

回答1:

Make sure that File's Owner in the nib is set to NSObject and also make sure that there are no outlets wired up to File's Owner.



回答2:

Check all of the elements in your nib to be sure their not referencing something that no longer exists. Right click on them to see what is pointing at what. There will be something in there for sure.



回答3:

Looks like you have linked a UILabel in your nib with an IBOutlet that is not existing anymore in your code (descriptionLabel).

So check your nib file again. I had this error several times too and this was the solution for me.



回答4:

I had exactly the same error and was wondering why my IBOutlets from the .h file weren't showing when I right-clicked the Table Cell in "Objects" on the left, but only at File's Owner. I found out that when I left clicked on my custom table cell in the "view" of the xib file and then in the attributes inspector assigned it to the correct custom (tableViewCell) class at "identifier", the outlets showed up at Table Cell at "Objects". Maybe this helps!



回答5:

In my case problem was solved by checking presence of .m in list of compilable sources.

If you rename, move or add new sources for custom table cell (or anything else needed by Interface Builder) you should add them to Project -> Targets -> -> Build Phases -> Compile Sources.

If you have more than one target - check all targets. In my project there was active target with type of Aggregate (with custom IPA building script), and i think this the root of problem because newly added *.m files misses Compile Sources list for Application-type target.



回答6:

static NSString *CellIdentifier = @"CellIdentifierName";
ContentPackCell *cell = (ContentPackCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"ContentPackCell" owner:self options:nil] ;
    cell=objCustomCell;
}


回答7:

Have you tried like this,

ContentPackCell *cell = (ContentPackCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
  [self.cellNib instantiateWithOwner:nil options:nil]; //Make owner nil
  cell = tmpCell;
  self.tmpCell = nil;
}

Usually when you do [[NSBundle mainBundle] loadNibNamed:@"ContentPackCell" owner:nil options:nil]; you get errors like what you get now. So if you getting error like "this class is not key value coding-compliant", in both the cases the reason is Class is not set for "File Owner" of Custom cell.