9

I've looked at almost all of the related questions about this here in stackoverflow and tried all their possible solutions but I still can't figure out why I get this error:

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

I'm trying to load a customized table view cell with the nibname: @"CustomCell". Its class is assigned to "CustomCell" (same class name as the nib name). Its file's owner is also set to the class which loads this cells - "ProductsTableViewControllerWithSearch". All the outlets in the nib are connected to the ones in CustomCell.m

Here's my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellClassName = @"CustomCell";
    static NSString *CellIdentifier = @"Custom Items Cell";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {

      NSArray *topLevelItems =  [[UINib nibWithNibName:CellClassName bundle:[NSBundle mainBundle]] instantiateWithOwner:self options:nil];
        cell = [topLevelItems objectAtIndex:0];

    }
//... more irrelevant code here
}

Can somebody help me please. I've been working on this for more than 4 hours. Thanks a lot!

PS: I'm using ARC and am developing for iOS 5.

Here's my CustomCell.h.

#import <UIKit/UIKit.h>

@interface CustomCell : UITableViewCell{
    UILabel *textLabel;
    UILabel *detailTextLabel;
    UIButton *starButton;
}

@property (strong, nonatomic) IBOutlet UILabel *textLabel;
@property (strong, nonatomic) IBOutlet UILabel *detailTextLabel;
@property (strong, nonatomic) IBOutlet UIButton *starButton;

@end

There's nothing in my CustomCell.m

acecapades
  • 893
  • 12
  • 23

4 Answers4

13

This error occurs if you are linking up any thing that does not belongs to that view.

In this case, the solution is in Interface Builder

  1. First Change the File's Owner Identity Class to NSObject.
  2. Unlink every IBOutlet in File's Owner.
  3. Change the Identity Class of Your custom NIBs to the name of CustomTableViewCell class.
  4. Link all your IBOutlet here.

That's it.

Black Tiger
  • 1,201
  • 11
  • 12
  • If you are loading the nib directly in your ViewController code and want to be able write the following code. `MyCustomCell *cell; cell.customTextField.text.delegate = self;` This will work for you. Also I think it makes more sense than the other current answers. – Evan Anger Nov 22 '12 at 17:33
  • 9
    Your CustomTablecell's UILabels should be connected to the UITableViewCell object, not the File's owner. I forget this every time. I am not sure why. – user798719 Dec 13 '12 at 05:42
  • This should be the accepted answer here - at least it solved the problem I had. – Gereon Dec 24 '13 at 11:59
9

In Interface Builder you need to set the class of the cell to CustomCell and CustomCell needs to have a property title.

this class is not key value coding-compliant for the key …

Usually means that a property is not found on a class and Interface Builder is trying to use that property, resulting in a crash.

fearmint
  • 5,276
  • 2
  • 33
  • 45
  • Yeah, I already set the CustomCell.xib's class to CustomCell... That's why I was able to connect all the UILabels in CustomCell.m. – acecapades Mar 15 '12 at 03:51
  • why does it need to have a property "title"? – acecapades Mar 15 '12 at 03:57
  • 1
    …I don’t know. Hmm. For “grown up” views usually everyone has a title, I don’t know when/where table view cells use their title. – fearmint Mar 15 '12 at 03:59
  • I see, that (declaring a property called title) solved it! – acecapades Mar 15 '12 at 04:08
  • But! That doesn’t solve the *root issue*. UITableView cell inherits from UIView, which doesn’t have a title property if I recall. UIViewController does—is your cell being referenced in two places one of which as a view controller? – fearmint Mar 15 '12 at 04:19
  • Oh I see. I'm not sure why it's like that. I only call my CustomCell in the cellForRowAtIndexPath of my ProductsTableViewController (UITVC subclass) - not in any other class. – acecapades Mar 15 '12 at 05:56
  • What’s being stored in title? – fearmint Mar 15 '12 at 12:30
  • sorry for the late reply, I'll try it out then I'll get back to you. thanks! – acecapades Mar 16 '12 at 07:59
6

I'm presuming that you get this error when your cell is loaded from the NIB at runtime. (If instead it's breaking on some line of code you've written, for God's sake delete that line of code!)

Go into your custom cell's XIB view, ctrl-click on "File's owner" and unlink whatever connection you've got in there called 'title'. If you're feeling spunky, unlink everything, then relink the stuff you want. See if that clears it up. If not I'll be here for a few hours.

QED
  • 9,803
  • 7
  • 50
  • 87
  • 1
    Good strategy—see if a ‘title’ property *is* wired up. – fearmint Mar 15 '12 at 04:01
  • This is almost always the cause in this scenario! – lnafziger Mar 15 '12 at 04:02
  • Yeah the cell is loaded from the NIB runtime. It's working now when I declared a property called 'title' in the header file. I didn't know that a property called title has to be declared. Thanks @JoePasq. I didn't see that in all the other questions related to this one here in StackOverflow. – acecapades Mar 15 '12 at 04:07
  • 2
    Add a `-setTitle:` method and log/breakpoint what the title is set to. – fearmint Mar 15 '12 at 04:20
  • 1
    I was having this error because I was missing an implementation: '@implementation YourTableCellClass '@end – amay0048 Jun 23 '14 at 15:13
  • 1
    Ctrl-Click the prototype helped me. I had a bad outlet variable name with a reserved keyword. Otherwise the class/etc was perfect, but I still got the error presumably because the class couldn't be loaded. – Paul May 30 '20 at 15:57
0

For my case of XCode6.0.1 ,I clean the project and everything runs smoothly

Lanston
  • 11,354
  • 8
  • 32
  • 37