简体   繁体   English

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

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

I have a project where I need to use a custom UITableViewCell. 我有一个项目,需要使用自定义UITableViewCell。 I'm designing the cell as a prototype in storyboard and it looks fine there. 我正在将该单元设计为情节提要中的原型,在那里看起来还不错。 I assign the prototype to my custom UITableViewCell subclass, give it the same reuse identifier I'm using in my UITableView and link the UILabel on the prototype cell to an IBOutlet in my UITableViewCell subclass. 我将原型分配给自定义UITableViewCell子类,并为其提供与我在UITableView中使用的相同的重用标识符,并将原型单元上的UILabel链接到UITableViewCell子类中的IBOutlet。

When I call it from the UITableView the cell is created and if I add labels and buttons in the code of that class (create them with a CGRect and all) they all work but the labels I've added in the storyboard never show up. 当我从UITableView调用它时,将创建单元格,并且如果在该类的代码中添加标签和按钮(使用CGRect等创建),它们都可以工作,但是我在情节提要中添加的标签永远不会显示。

I don't understand how my subclass can be called and created successfully but its layout and subviews from the storyboard don't seem to exist as far as my app is concerned. 我不知道如何成功调用和创建我的子类,但是就我的应用而言,它的布局和情节提要中的子视图似乎并不存在。 What am I doing wrong? 我究竟做错了什么?

Here's my cellForRowAtIndexPath code 这是我的cellForRowAtIndexPath代码

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

    // Configure the cell...

    return cell;
}

I've run into this issue before, and in my case, the problem was that the auto-generated code for the view controller included a call to: 我之前就遇到过这个问题,就我而言,问题是视图控制器的自动生成的代码包括对以下内容的调用:

[UITableView registerClass:forCellReuseIdentifier:]

I would suggest checking for and removing any calls to the above, or to 我建议检查并删除对上述内容的任何调用,或

[UITableView registerNib:forCellReuseIdentifier:]

and trying your original code again. 然后再次尝试您的原始代码。

Your cellForIndexViewPath should look like this, to create a simple custom cell with label, 您的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;
}

Make sure that you have made all connections well, set datasource and delegate to table and then set the “Identifier” of the custom cell to "MyTableViewCell" in “Attributes Inspector” like this, 确保已经建立了所有连接,将datasourcedelegate设置为table ,然后在“属性检查器”中将custom cell的“标识符”设置为“ MyTableViewCell”,如下所示:

For storyboard: 对于情节提要:

在此处输入图片说明

Add "MyTableViewCell" instead of "SimpleTableCell" as shown in above screenshot. 如上屏幕截图所示,添加“ MyTableViewCell”而不是“ SimpleTableCell”。

acreichman, add casting in cellForRow and put an NSLog in you cell's awakeFromNib to see if you get there. acreichman,在cellForRow中添加强制转换,然后在单元格的awakeFromNib中放置一个NSLog,以查看是否到达那里。 Let me know... 让我知道...

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

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