Search code examples
iosxcodeuitableviewcell

Custom TableViewCell - this class is not key value coding-compliant for the key


Im trying to use my custom cell but it doesnt work.

Here's the error:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UITableViewCell 0x12ce3dcf0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key anmerkungLabel.'
*** First throw call stack:
(0x182fb259c 0x1937080e4 0x182fb2220 0x183dbdb10 0x187a305d0 0x182edf3c0 0x187b6f83c 0x1878a28dc 0x10003d77c 0x187a5aef0 0x187a4faa4 0x187844ce8 0x187761648 0x1870b9994 0x1870b4564 0x187775a2c 0x18789c194 0x18789b61c 0x18789b304 0x187964ed8 0x187798d34 0x187964d44 0x187798d34 0x187781e48 0x18796497c 0x187798d34 0x187781e48 0x1877986d0 0x18779835c 0x1877918b0 0x187764fa8 0x187a03f58 0x187763510 0x182f6a9ec 0x182f69c90 0x182f67d40 0x182e950a4 0x18c0375a4 0x1877ca3c0 0x100058498 0x193d76a08)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

My cell is called: VertretungsCell

VertretungsCell.h:

#import <UIKit/UIKit.h>

@interface VertretungsCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *stundeLabel;
@property (weak, nonatomic) IBOutlet UILabel *stattLabel;
@property (weak, nonatomic) IBOutlet UILabel *fachLabel;
@property (weak, nonatomic) IBOutlet UILabel *raumLabel;
@property (weak, nonatomic) IBOutlet UILabel *anmerkungLabel;

@end

VertretungsCell.h:

#import "VertretungsCell.h"

@implementation VertretungsCell
@synthesize stundeLabel,stattLabel,fachLabel,raumLabel,anmerkungLabel;

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

The ViewController is called: Vertretungsplan

Vertretungsplan.h:

#import <UIKit/UIKit.h>

@interface Vertretungsplan : UIViewController <UITableViewDataSource,UITableViewDelegate>

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

@end

Vertreutungsplan.m:

#import "Vertretungsplan.h"
#import "VertretungsCell.h"

@interface Vertretungsplan ()

@end

@implementation Vertretungsplan {
    NSMutableArray *stunde;
    NSMutableArray *statt;
    NSMutableArray *fach;
    NSMutableArray *raum;
    NSMutableArray *anmerkung;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    stunde = [[NSMutableArray alloc] initWithArray:[NSArray arrayWithObjects: @"1",@"2",@"3", nil]];
    statt = [[NSMutableArray alloc] initWithArray:[NSArray arrayWithObjects: @"D1",@"E2",@"M3", nil]];
    fach = [[NSMutableArray alloc] initWithArray:[NSArray arrayWithObjects: @"CH1",@"PP2",@"SP3", nil]];
    raum = [[NSMutableArray alloc] initWithArray:[NSArray arrayWithObjects: @"111",@"222",@"333", nil]];
    anmerkung = [[NSMutableArray alloc] initWithArray:[NSArray arrayWithObjects: @"EVA",@"1. fällt aus",@"EVA", nil]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return stunde.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[VertretungsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    VertretungsCell *vertretungsCell = (VertretungsCell *)cell;

    vertretungsCell.stundeLabel.text = stunde[indexPath.row];
    vertretungsCell.stattLabel.text = statt[indexPath.row];
    vertretungsCell.fachLabel.text = fach[indexPath.row];
    vertretungsCell.raumLabel.text = raum[indexPath.row];
    vertretungsCell.anmerkungLabel.text = anmerkung[indexPath.row];

    return cell;
}

@end

Here are some screenshots: (may help)

Aspect Cell Identity


Solution

  • You need to check the IBOutlet connection for anmerkungLabel in your storyboard. Note: You don't need to check if the cell equals nil if your using storyboad. Just in your cellForRowAtIndex path use VertretungsCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath].