UIStoryboard中的自定义原型单元已创建,但未显示在UITableView中

时间:2015-01-22 08:49:00

标签: ios objective-c uitableview uistoryboard

我有一个需要使用自定义UITableViewCell的项目。我正在设计单元格作为故事板中的原型,它在那里看起来很好。我将原型分配给我的自定义UITableViewCell子类,给它在我的UITableView中使用相同的重用标识符,并将原型单元格上的UILabel链接到我的UITableViewCell子类中的IBOutlet。

当我从UITableView调用它时,单元格被创建,如果我在该类的代码中添加标签和按钮(使用CGRect创建它们),它们都可以工作,但我在故事板中添加的标签从不出现。

我不明白我的子类是如何被成功调用和创建的,但是就我的应用而言,它的故事板中的布局和子视图似乎并不存在。我做错了什么?

这是我的cellForRowAtIndexPath代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    // Configure the cell...

    return cell;
}

3 个答案:

答案 0 :(得分:3)

之前我遇到过这个问题,在我的情况下,问题是视图控制器的自动生成代码包含对以下内容的调用:

[UITableView registerClass:forCellReuseIdentifier:]

我建议检查并删除对上述内容的任何调用,或者

[UITableView registerNib:forCellReuseIdentifier:]

再次尝试原始代码。

答案 1 :(得分:0)

您的cellForIndexViewPath应如下所示,创建一个带标签的简单custom cell

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

    SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil) 
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    } 
    cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
    return cell;
}

确保您已完成所有连接,将datasourcedelegate设置为table,然后将custom cell的“标识符”设置为“MyTableViewCell” “属性检查器”就像这样,

对于故事板:

enter image description here

添加“MyTableViewCell”而不是“SimpleTableCell”,如上面的屏幕截图所示。

答案 2 :(得分:0)

acreichman,在cellForRow中添加强制转换,并在你的单元格的awakeFromNib中放置一个NSLog,看看你是否到达那里。让我知道......