Squatch
Squatch

Reputation: 1027

Custom table cell: "class is not key value coding-compliant"

UPDATE: I decided to start over since I was still in the early stages of this app. I repeated everything and for whatever reason, the custom cell took the second time around. I will keep the old files around to confirm another answer, as I imagine I am not the only one who will have this problem.

I am building a tabbed application that requires custom cells in its table views. I have done this a few times and I always seem to hit a speed bump when hooking up these custom cells. The app booted up fine until I started using the custom cell in my table view controller titled SongsTVC. I am receiving a termination with a reason of:

[<SongsTVC 0x6831330> setValue:forUndefinedKey:]: 
this class is not key value coding-compliant for the key albumLabel.

I am using this tutorial and have used it before (changed a few things for ARC and iOS 5) with success. In fact, the code and IB layout I am using is based off of an already working project I have. I am aware of this error commonly presenting itself when you hook up your outlets to the file's owner and not the cell itself. I am not making this mistake but it is still giving me this error. So far, I have removed the label it has a problem with and even deleted the cell's files entirely in order to start over. Any help would be appreciated.

SongCell.h

#import <UIKit/UIKit.h>

@interface SongCell : UITableViewCell{

}

@property(nonatomic, assign) IBOutlet UILabel *titleLabel;
@property(nonatomic, assign) IBOutlet UILabel *artistLabel;
@property(nonatomic, assign) IBOutlet UILabel *albumLabel;

@end

SongCell.m

#import "SongCell.h"

@interface SongCell ()

@end

@implementation SongCell

@synthesize titleLabel, artistLabel, albumLabel;

@end

SongsTVC.h - Header of the TableViewController

#import <UIKit/UIKit.h>

@interface SongsTVC : UITableViewController{
    UINib *cellLoader;
}

@end

SongsTVC.m - Relevant TableViewController methods

#import "SongsTVC.h"
#import "SongCell.h"

@interface SongsTVC ()

@end

static NSString *CellClassName = @"SongCell";

@implementation SongsTVC

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
        cellLoader = [UINib nibWithNibName:CellClassName bundle:[NSBundle mainBundle]];
    }
    return self;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SongCell *cell = (SongCell *)[tableView dequeueReusableCellWithIdentifier:CellClassName];

    if (!cell)
    {
        //CRASH ERROR POINTS HERE
        NSArray *topLevelItems = [cellLoader instantiateWithOwner:self options:nil];
        cell = [topLevelItems objectAtIndex:0];
    }

    // Configure the cell...
    cell.titleLabel.text = @"SONG";
    cell.artistLabel.text = @"Artist";
    cell.albumLabel.text = @"Album";

    return cell;
}

Interface Builder The cell, its outlets, and its class


File's owner

Report

NOTE: The cell identifier has been set to "SongCell" in IB and the file owner is UITableViewController because multiple tables will be using this cell. As long as the view is a table, it should work (it has in the past).


UPDATE: The xib file in XML format has been pasted here.

Upvotes: 2

Views: 2881

Answers (3)

Benjamin Stark
Benjamin Stark

Reputation: 440

I had a similar issue and was able to solve it by rebuilding the Storyboard/NIB.

Upvotes: 0

Squatch
Squatch

Reputation: 1027

Unfortunately, the only solution that I have found was to start over. I did everything exactly as I did before and it worked the second time around. It was quite a chore having to scrap the entire thing and start over but it was the only way I could get it to work. I'm leaving this as the answer unless somebody can figure out what happened. (See original post and its update)

Upvotes: 1

jrturton
jrturton

Reputation: 119292

I had this exact problem today, while making a sample project for another SO answer! It looks like your xib is trying to connect an outlet to your view controller instead of your cell. In your screenshot, the outlets look correctly defined, but occasionally an old outlet definition can get left in the underlying XML and cause this type of crash.

If you've changed the files owner class after connecting some outlets, for example, this could confuse it.

You may be able to find it by opening the xib as "source code", look for the element and check there are only the entries you expect. Perhaps search the XML file for albumLabel as well.

If that doesn't work, you may have to scrap the xib and start again.

Upvotes: 2

Related Questions