开发者

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:CellId开发者_运维百科entifier];
    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:

error with custom UITableViewCell


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!!!


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


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.


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


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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜