自定义tableviewcell错误

时间:2012-01-30 09:11:06

标签: objective-c uitableview

我想使用自定义tableview Cell。

我有productTableViewCell类。在我的xib中我定义了一个lblProduct,在这个标签上我想显示我的产品名称。我已经合成它并在我的xib中正确连接它。

现在我的另一个课程中有我的tableview。我这样做我的所有产品都在arrayProducts中。

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

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

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

        for (UIView *view in views) {
            if([view isKindOfClass:[UITableViewCell class]])
            {   
                cell = (productTableviewCell*)view;
            }
        }
    }
    NSString *cellValue = [arrayProducts objectAtIndex:indexPath.row];
    NSLog(@"value: %@", cellValue);
    cell.lblProduct.text = cellValue;

    //  NSDictionary *info = [json objectAtIndex:indexPath.row];
    //cell.lblProduct.text = [info objectForKey:@"Pro_naam"];

    return cell;

}

但是当我跑的时候。我收到这个错误。

  

2012-01-30 10:02:40.032 MamzelBestelling2 [12923:f803] *终止   应用程序由于未捕获的异常'NSUnknownKeyException',原因:   '[setValue:forUndefinedKey:]:这个类不是   密钥lblProduct的密钥值编码兼容。   * 第一次抛出调用堆栈:(0x13c0052 0x1551d0a 0x13bff11 0x9b7032 0x928f7b 0x928eeb 0x943d60 0x23691a 0x13c1e1a 0x132b821 0x23546e   0x237010 0x4d9f 0xb0e0f 0xb1589 0x9cdfd 0xab851 0x56301 0x13c1e72   0x1d6a92d 0x1d74827 0x1cfafa7 0x1cfcea6 0x1cfc580 0x13949ce 0x132b670   0x12f74f6 0x12f6db4 0x12f6ccb 0x12a9879 0x12a993e 0x17a9b 0x2148   0x20a5)终止调用抛出异常当前语言:auto;   目前objective-c(gdb)

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

你真的不需要先将它存储到NSString中。试着这样做:

cell.lblProduct.text = [arrayProducts objectAtIndex:indexPath.row];

另外,请确保您已正确完成@property@synthesize并在CustomCell xib中将它们连接起来

答案 1 :(得分:0)

如果您创建了CustomTableViewCell,则必须为“lblProduct”设置标记,比如100。然后 做

UILabel *label=(UILabel *)[cell viewWithTag:100];
NSString *cellValue = [arrayProducts objectAtIndex:indexPath.row];
NSLog(@"value: %@", cellValue);
lblProduct.text = cellValue;

return cell;

它会起作用!!!!

有关详细信息,请http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW1

相关问题