aherlambang
aherlambang

Reputation: 14418

error with custom UITableViewCell

I am getting this error:

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

Following is the code:

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

    ReservationCell *cell = (ReservationCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ReservationCell" owner:nil options:nil];
        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell = (ReservationCell *) currentObject;
                break;
            }
        }
    }


    //cell.origin.text = [[data objectAtIndex:indexPath.row] origin];
    //cell.destination.text = [[data objectAtIndex:indexPath.row] destination]; 
    //cell.time_range.text = [[data objectAtIndex:indexPath.row] time_range]; 

    return cell;
}

Here is the ReservationCell.h

@interface ReservationCell : UITableViewCell {
    UILabel * origin;
    UILabel * destination;
    UILabel * time_range;
}

@property (nonatomic, retain) IBOutlet UILabel * origin;
@property (nonatomic, retain) IBOutlet UILabel * destination;
@property (nonatomic, retain) IBOutlet UILabel * time_range;

@end

Here's how I wired it up: enter image description here

Upvotes: 4

Views: 4058

Answers (6)

skymatt70
skymatt70

Reputation: 63

In my case I had added the interface for the cell to the header file but did not have an implementation in the .m file. Simply adding an empty implementation (as below) for the class to the .m file fixed the problem.

@implementation myTableViewCell
@end

Surprised this has not been listed as an answer as this has bitten me several times in the past. Hope this helps someone!

Upvotes: 1

horkavlna
horkavlna

Reputation: 3042

My exception was 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key (and name label which I had on the CustomCell, I got this error only when I added something in CustomCell e.g. label)

Solution problem my friend gave me great advice add MyCustomCell.m add to the ProjectSettings -> Build Phases -> add MyCustomCell.m file

Upvotes: 4

codesnooker
codesnooker

Reputation: 1181

For someone, who already reached to this thread and not able to figure out the solution like me, here is the quick solution to this problem:

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.

Good Luck!!!

Upvotes: 10

Borislav Gizdov
Borislav Gizdov

Reputation: 1950

This problem happen when in the interface builder for the CustomCell nib, the File's Owner custom class is set to your CustomCell class.

  1. File's owner custom class should be set always UITableViewCell.
  2. Table view object custom class should be set to your CustomCell class.

Also you need to initialize it with Nib name of your tableview cell.

Example (NotificationCell is my custom cell class): http://i42.tinypic.com/29pw0a8.png

Upvotes: 7

nh32rg
nh32rg

Reputation: 1482

I had this problem when I had duplicated a prototype cell in storyboard and deleted one of the outlets in the cell. It left a reference to the outlet but not connected to anything. Right-click on the prototype cell and look for one of those yellow warning markers.

Upvotes: 0

Jason Harwig
Jason Harwig

Reputation: 45551

NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ReservationCell"
                                                         owner:nil 
                                                       options:nil];

Is setting the Files Owner to nil. So you can't wire any of your labels to that. Instead, make sure the class of the cell is ReservationCell and its outlets are connected to the labels.

Upvotes: 4

Related Questions