33

First of all, this discussion did not solve my problem.

Custom UITableViewCell subclass: This class is not a key value coding-compliant

Setup:

I have an array of Person objects in MainViewController. Want to show the objects in an UITableView in AllContactsViewController.

Problem:

All works as expected when use the default UITableViewCell. When I use my custom TableCell I get an error pointing to that this class is not key value coding-compliant.

This error occurs right after I connect ANY of my three outlets in IB with my TableCell Class.

Note:

TableCell has three properties: a name & email label + imageView.

Hope you can help.

TableCell.h

#import <UIKit/UIKit.h>

@interface TableCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *emailLabel;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

TableCell.m

#import "TableCell.h"

@implementation TableCell
@synthesize nameLabel, imageView, emailLabel;

AllContactsViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdent = @"TableCell";

    TableCell *cell = (TableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdent];

    if (cell == nil) {

        NSArray *array = [[NSBundle mainBundle]loadNibNamed:@"TableCell" owner:self options:nil];
        cell = [array objectAtIndex:0];
    }

    NSString *firstAndLastnameString =
    [NSString stringWithFormat:@"%@ %@",
     [(NewContact *)[self.allContactsArray objectAtIndex:[indexPath row]] firstName],
     [(NewContact *)[self.allContactsArray objectAtIndex:[indexPath row]] lastName]];

    cell.nameLabel.text = firstAndLastnameString;

    cell.emailLabel.text =
    [(NewContact *)[self.allContactsArray objectAtIndex:[indexPath row]] eMail];

    cell.imageView.image =
    [(NewContact *)[self.allContactsArray objectAtIndex:[indexPath row]] contactPicture];

    return cell;
}

UPDATE:

Maybe a screenshot will help from IB.

enter image description here

Added full AllContactsViewController.h

#import <UIKit/UIKit.h>
#import "VCDelegate.h"
#import "TableCell.h"

@interface AllContactsViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) id <VCDelegate>delegate;

@property (weak, nonatomic) IBOutlet UITableView *allContactsTableView;

@property (strong, nonatomic) NSMutableArray *allContactsArray;

@property (assign) int currentArrayIndexNumber;

- (IBAction)closeBtnTapped:(id)sender;

@end
Community
  • 1
  • 1
  • 1
    Not key-value-compliant for which key? Give the exact error message. - Did you verify that `cell = [array objectAtIndex:0]` actually returns an instance of your `TableCell` class? – Martin R Aug 16 '12 at 10:49
  • Depends on the `outlet` I connect in IB. I connect the `name` `label`, app crashes, I connect the `imageView` instead the app crashes. Looks like I have an issue with ALL properties I declared in `TableCell` I'm sure I get an instance back of `TableCell` otherwise the error message must be different since the error points to the outlets I have in `TableCell` - Exact error message: `[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key imageView.'` –  Aug 16 '12 at 10:55
  • The error message points to `AllContactsViewController`! Add `NSLog(@"class=%@", [cell class]);` to your code to check. – Martin R Aug 16 '12 at 10:59
  • Console output: 2012-08-16 14:12:57.553 Contacts+[15330:c07] class=TableCell –  Aug 16 '12 at 12:13
  • 1
    Check out this answer http://stackoverflow.com/a/13300245/163936 – Bo. Apr 01 '13 at 10:15

6 Answers6

67

If you are connecting the outlets from the file's owner, you should do different:

1 - Define the class of the UITableViewCell to your TableCell class:

enter image description here

2 - Connect the outlets not from File's Owner, but from TableCell in the objects list:

Connect the outlets

Natan R.
  • 5,141
  • 1
  • 31
  • 48
  • In IB (Identity Insp.) I have set the `class` already to `TableCell`. So when I ctrl click the File's Owner I see the `outlets` I have declared in `TableCell` –  Aug 16 '12 at 12:16
  • Hi Natan, unfortunately no, I went to IB set the `TableViewCELL` to my custom `class`. CTRL clicked the `cell` and connected the `outlets` but same error. I have edited my original post and added a screenshot. Maybe it helps. –  Aug 16 '12 at 15:18
  • Go to your AllContactsViewController and check if you don't have any outlet connected to something but in fact the outlet maybe doesn't exists. – Natan R. Aug 16 '12 at 15:26
  • The only IBOutlet in AllContactsViewController I have is the UITableView. I have updated my original post with whole AllViewController.h file. –  Aug 16 '12 at 15:33
  • `The only IBOutlet in AllContactsViewController I have is the UITableView` : this in your .xib file or in the header? When I said `Go to your AllContactsViewController` I was refering to open it in Interface Builder. – Natan R. Aug 16 '12 at 15:38
  • 3
    Thanks, connecting the outlet from the TableCell rather than from File's Owner solved this problem for me, too. Is there a reason why it has to be done this way? I've made this mistake a couple of times actually, so I wonder why Xcode doesn't automatically create a UITableViewCell subclass and .xib such that you can connect new components in a custom cell in the same way that other UI components are connected with Interface Builder? – Wade Apr 18 '14 at 17:39
  • I second Wade's motion! I have also fallen on this more than once - you need to connect with the Table Cell not with the File Owner. – Andy Weinstein Nov 30 '20 at 18:28
5

Everything was done right on my project and still received when try to use custom cell with its properties. Here how I solved mine.

CustomUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomUITableViewCell" forIndexPath:indexPath];

What actually hit me is simply remove the CustomUITableViewCell Class from the custom cell's "Identity Inspector" and re-enter it.. you will see a changes

  1. Module value
  2. Inherit Module From Target

Let them be the value which automatically comes up... don't try to inherit it or change the inheritance or the Module value...

enter image description here

  • 1
    Actually, I did what you said in reentering the class of my cell, it automatically checked the inherit module from target box -- which was previously unchecked -- and this fixed my problem. No idea why. – chaytan Dec 22 '20 at 18:11
2

If you're using Xcode 6 GM, there's a bug in IB that may result this error. I don't think the OP was having this problem because it's more obvious (you can see a "undefined class MyXXXCell" in log) but in cause you get to this question from google like me, just try to enter the class name in IB and hit enter again and once it's really saved in the storyboard/xml file it will be fine.

superarts.org
  • 7,009
  • 1
  • 58
  • 44
1

I had the same problem and any of the comments here or in other discussions helped me...at the end I just made a clean cause I was pretty sure I was doing everything correctly and yes...magically worked!

Joan Cardona
  • 3,463
  • 2
  • 25
  • 43
1

Had the same issue using Xcode 12. It worked by simply retyping the class on the Identity Inspector. Seems like a bug.

ccast25
  • 11
  • 3
0

This happened to me just after Git merge and resolving few conflicts. It had screwed up my XCode project.

I tried all over the StackOverflow. At first I thought my Custom class .m file hasn't been added to the Compile Sources class list. But it's one case, however even after adding it, it gave me same issue back.

How I resolved:

  1. I kept a backup of my .xib file and its backing custom class .m and .h files.
  2. Then clean the project and try to run the app via XCode (which might not work)
  3. Then re-add the files back to the XCode project and it will work.

Cheers!

Randika Vishman
  • 7,983
  • 3
  • 57
  • 80