user1010563
user1010563

Reputation:

Custom UITableViewCell gives me: this class is not key value coding-compliant

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

Upvotes: 36

Views: 17001

Answers (7)

Plato
Plato

Reputation: 641

In my case, I was using a Nib from a different module, but the Nib had an incorrect "Module" listed under "Custom Class" in the Nib Identity Inspector. It was checked to "Inherit Module From Target", but instead I had to manually set the Nib's module to the specific sub-module that it is located within.

Upvotes: 0

cca0125
cca0125

Reputation: 11

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

Upvotes: 1

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

Upvotes: 6

Randika Vishman
Randika Vishman

Reputation: 8124

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!

Upvotes: 0

Joan Cardona
Joan Cardona

Reputation: 3691

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!

Upvotes: 1

superarts.org
superarts.org

Reputation: 7238

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.

Upvotes: 2

Natan R.
Natan R.

Reputation: 5181

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

Upvotes: 68

Related Questions