繁体   English   中英

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

[英]Custom prototype cell in UIStoryboard is created but doesn't show up in UITableView

我有一个项目,需要使用自定义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;
}

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

[UITableView registerClass:forCellReuseIdentifier:]

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

[UITableView registerNib:forCellReuseIdentifier:]

然后再次尝试您的原始代码。

您的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”,如下所示:

对于情节提要:

在此处输入图片说明

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM