8

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

Setting the custom class in IB

Setting an Identifier in IB


Update:

Setting the class for File's Owner

sam2themax
  • 1,068
  • 3
  • 10
  • 14
  • I'm pretty well ready to give up on this and just go back to the old way of using the loadNibNamed method of NSBundle. It seems to work just fine for my case. – sam2themax Sep 07 '12 at 00:19
  • I found if I right clicked on my custom cell label and drag to appropriate .h file, to create an outlet, the outlet was assigned to the File's Owner and this causes the above crash. If instead I right click on the label and drag to the table cell (to the left) then select the appropriate outlet all is OK. – Nick T Mar 19 '13 at 20:16
  • @softwareevolved Agreed that looks like a better answer. I haven't gone back to try it, but I did vote to close my question as a duplicate of that one. Thanks! – sam2themax Jul 03 '13 at 12:44

7 Answers7

37

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.

Binyamin Bauman
  • 1,041
  • 10
  • 11
5

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.

AppHandwerker
  • 1,758
  • 12
  • 22
  • I don't think so. The issue is with a link to an existing IBOutlet, descriptionLabel. The problem seems to be that instantiateWithOwner is using UITableViewCell instead of my custom class. Since there is no IBOutlet for descriptionLabel in UITableViewCell, it blows up. If I delete the connection to my custom class's descriptionLabel, it doesn't blow up, but it also doesn't have that necessary connection. – sam2themax Aug 31 '12 at 16:07
2

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.

E. Lüders
  • 1,495
  • 1
  • 12
  • 27
1

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!

Linus
  • 4,643
  • 8
  • 49
  • 74
1

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.

  • Thank you for this comment. For some reason XCode failed to add the .m file for one of my custom cells to the build target even though it normally does when I add a new class. This fixed this issue for me. I probably would not have thought to look at this as a a potential cause. – mnemia Oct 03 '14 at 15:28
0
static NSString *CellIdentifier = @"CellIdentifierName";
ContentPackCell *cell = (ContentPackCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"ContentPackCell" owner:self options:nil] ;
    cell=objCustomCell;
}
Rostyslav Dzinko
  • 39,424
  • 5
  • 49
  • 62
Sameer
  • 1
  • I'm trying to do things in the new, more efficient way of reusing a UINib instance. So, I don't want to use loadNibNamed. I want to use instantiateWithOwner. – sam2themax Aug 31 '12 at 16:09
0

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.

rakeshNS
  • 4,227
  • 4
  • 28
  • 42
  • Yes, I get the same error when I try it that way, so I think you're on to something -- thank you. Unfortunately, I double-checked, and the class is set for the File's Owner of the cell. I updated my question with a screenshot showing this. – sam2themax Sep 06 '12 at 19:13