UITableViewCell不显示数据

时间:2010-10-12 14:37:07

标签: iphone objective-c uitableview

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

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if (cell == nil)
    {
        //cell = [[[CustomCell alloc] initwithidentifier:CustomCellIdentifier] autorelease];
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        for (id oneObject in nib)
            if ([oneObject isKindOfClass:[CustomCell class]]) {
                cell = (CustomCell *)oneObject;
            }
    }

    NSUInteger row = [indexPath row];
    NSDictionary *rowData = [self.listData objectAtIndex:row];
    cell.nameLabel.text = @"text";
    [cell.contentView addSubview: nameLabel];
    [cell reloadInputViews];

    return cell;
}

但名称Label不会显示其新名称。我做错了什么? 谢谢!

1 个答案:

答案 0 :(得分:1)

我愿意猜测,如果你检查cell.NameLabel,你会发现它是零 - 没有通过loadNibNamed:owner:options:正确连接IBOutlet。你确定CustomCell有nameLabel作为合适的插座(即键值编码可以达到的插座)吗? Apple的参考是here,但确保你有一个setter和getter就足够了。

在您创建NIB时,您可能认为CustomCell不是文件所有者吗?如果是则loadNibNamed:owner:options:显然不应该在self上调用。如果'self'是实际拥有者,那么拥有一个指向CustomCellView而不是通过返回数组捕获的IBOutlet(也绝对符合KVC)可能更为全面。

补充,在下面的评论讨论之后:

绝对有效的是使用合适的IBOutlet创建UITableViewCell的自定义子类,这些子类也是属性,将表视图数据源设置为文件所有者,并使用loadNib以类的形式加载NIB的新实例目标,导致您的插座附加新实例。然后,您可以使用子类的属性来访问单元格的各种子视图。示例项目:here