ios - 自定义 TableViewCell - 此类与键的键值编码不兼容

标签 ios xcode uitableview cell

我正在尝试使用我的自定义单元格,但它不起作用。

这是错误:

*** 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) 

我的手机名为: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

ViewController 称为: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

以下是一些截图:(可能有帮助)

Aspect Cell Identity

最佳答案

您需要在 Storyboard中检查 anmerkungLabel 的 IBOutlet 连接。 注意:如果您使用 Storyboard,则无需检查单元格是否等于 nil。就在您的 cellForRowAtIndex 路径中使用 VertretungsCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]

关于ios - 自定义 TableViewCell - 此类与键的键值编码不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28265060/

相关文章:

objective-c - 将 NavigationController 添加到当前 View

ios - TestFlight 中缺少合规性状态

xcode - 从当前位置知道 MapView 中最近的引脚

swift - 从 Xcode 按钮运行 Shell 脚本

swift - 使用可重用的 TableView 进行排序

ios - 如何在 Swift 包管理器中添加 Reality 文件?

ios - 在pin touch mapkit swift上绘制路线

ios - 在 GPUImageView 上找不到方法 "setImage:"

objective-c - SearchBar 与隐藏的 UITableView iOS

ios - TableView Segue 到新 ViewController 不能工作两次