Class is not key value coding-compliant

Rob picture Rob · Sep 21, 2010 · Viewed 59.4k times · Source

I know the meaning of this error, but I'm really struggling with it, and I need someone's help :

2010-09-21 15:03:11.562 Stocks[5605:207] *** Terminating app due to uncaught 
exception 'NSUnknownKeyException', reason: '[<NSObject 0x499fb20> 
setValue:forUndefinedKey:]: this class is not key value coding-compliant 
for the key actionText.'

There is my code here :

AlertCell.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>


@interface AlertCell : UITableViewCell {
    IBOutlet UILabel *actionText;
}

@property (retain, nonatomic) UILabel *actionText;

@end

And

AlertCell.m

@implementation AlertCell
@synthesize actionText;
   
- (void)dealloc {
    [actionText release];
    [super dealloc];
}
   
@end

The problem happens just there :

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    AlertCell *cell = 
      (AlertCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AlertCell" 
                                                         owner:nil 
                                                       options:nil];
        for (id oneObject in nib) {
            if ([oneObject isKindOfClass:[UITableViewCell class]]) {
                cell = (AlertCell *)oneObject;
                break;
            }
        }
    }    
    
    
    cell.actionText.text = [arrayAlert objectAtIndex:indexPath.row];
    return cell;
}

On this line :

NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AlertCell" 
                                             owner:nil 
                                           options:nil];

As asked, here is my header for the TableViewCOntroller :

#import <UIKit/UIKit.h>


@interface AlertesViewController : UITableViewController {
    NSMutableArray *arrayAlert;
}

And you can see my XIB file (as XML): http://pastebin.com/FDVzLYZu

@end

Can anyone help me ? Thanks a lot !

Answer

theprof picture theprof · Jul 21, 2011

you probably based your code on a web tutorial such as the one at http://www.e-string.com/content/custom-uitableviewcells-interface-builder or http://www.icodeblog.com/2009/05/24/custom-uitableviewcell-using-interface-builder/

Your problem (and I'm 99% sure this is where you tripped up, I just made the same mistake) is that in interface builder you linked your IBOutlets from File's Owner when you should link them from the cell view. This is why you are getting the errors.