Iphone 自定义单元格UITableview

Iphone 自定义单元格UITableview,iphone,objective-c,Iphone,Objective C,我想在UITableview中有一个自定义单元格。我尝试了以下方法: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWith

我想在UITableview中有一个自定义单元格。我尝试了以下方法:

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

CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {

    NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:nil options:nil];

    for (UIView *view in views) {
        if([view isKindOfClass:[UITableViewCell class]])
        {
            cell = (CustomCell*)view;
        }
    }
}
[cell setLabelText:[daten objectAtIndex:indexPath.row]];
return cell;

}
但我在这一行遇到了一个例外:

CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

这就是我如何制作自定义单元格并在课堂上调用它的方法:-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     {
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[cell reuseIdentifier]];

    if (cell == nil)
    {
        [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = customCell;
        customCell = nil;
    }

    // Configure the cell.
    cell.serialLabel.text = [[NSNumber numberWithInt:indexPath.row+1]stringValue];
    cell.textLabel.textColor=[UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    }

我几乎可以肯定,您在XIB文件中已失去连接。如果commentLabel仍然存在,您应该删除它并创建一个新的插座,或者检查插座部分并删除额外的连接,一切都将正常

编辑: 当您将两个插座连接到IB中的同一标签时,会发生此错误


如果您没有使用最新的ios SDK,请确保您已编写 @综合标签;
在CustomCell中

尝试清理生成文件夹,或尝试模拟器->重置内容和设置。

粘贴异常日志。您是否在其他类中创建了自定义单元格??2012-09-24 12:30:33.514 Speedcheck[1183:907]***由于未捕获的异常“NSUnknownKeyException”终止应用程序,原因:'[setValue:forUndefinedKey:]:此类不符合key commentLabel的键值编码。“***第一次抛出调用堆栈:(0x34d532a3 0x3452597f 0x34d52f99 0x39caf9d1 0x37a2f1e1 0x39cab723 0x34cd961b 0x37a28245 0x378d672f 0xd8b27 0x378d654d 0x378BB130x378D27CF 0x3788e80b 0x33f05901 0x33f06835 0x33f0621b 0x33f06029 0x378948f3 0x34d286cd 0x34d269c1 0x34d26d17 0x34c99ebd 0x3499D49 0x35A0220B3013 EB9130)lib在
cell=(CustomCell*)上放置断点查看
。它是否返回一个单元格?nib文件中是否设置了单元格的类?不,它不返回单元格,因为我之前遇到了一个异常…是的,该类在我的故事板中设置了!该类与key commentLabel的键值编码不兼容。您的错误出现在此处…这意味着您没有为您的commentLabel包含该类。…检查此项我不太确定您的意思!您的commentLabel是否通过IBOutlet附加到您的xib文件?但现在我收到此异常:2012-09-24 12:52:07.761 Speedcheck[1279:907]-[Daten IsequalString::]:未识别的选择器发送到实例0x1e5903d0 2012-09-24 12:52:07.771 Speedcheck[1279:907]***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[Daten isEqualToString::]:未识别的选择器发送到实例0x1E590300'***第一次抛出调用堆栈:(0x34d532a3 0x3452597f 0x34d56e07 0x34d55531 0x34cacf68 0x3788f37d 0x7a6df 0x79d2d 0x378d654d 0x378bb313 0x378d27cf 0x3788e80b 0x33f05d63 0x33f05901 0x33f06835 0x33f0621b 0x33f06029 0x378948f3 0x34D286CDU是否已将其与代理连接?yee已双重连接…已解决第一个异常…但现在我有另一个异常,因为您可以n看上面!