繁体   English   中英

UITableView接收自定义UITableViewCell,但未显示任何内容

[英]UITableView receives custom UITableViewCell but nothing is shown

我正在使用自定义TableViewCell测试UITableView。 自定义单元格是在.xib文件中设计的,并具有自己的名为cell的类,它是UITableViewCell的子类:

细胞

#import "Cell.h"

@implementation Cell

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    return self;
}

@end

ViewController类中的UITableView名为table。 它也是它的代理和数据源。 两者都设置在情节提要中。 其代码如下所示:

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.table registerClass:[Cell class] forCellReuseIdentifier:@"cell" ];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [self.table dequeueReusableCellWithIdentifier:@"cell"];
    return cell;
}

@end

如果运行该应用程序,则可以看到该表,并且该表具有正确的单元格数量。 但是未显示自定义单元格。 表格的每个单元格都是白色。

我读了几篇关于此类问题的文章,但没人帮我。 不同的教程说它应该像这样工作,但事实并非如此。 我认为其中可能有一个愚蠢的错误。 谢谢你的帮助。

我猜您忘了在* .xib文件中设置您的自定义类名称。 在* .xib属性中将“自定义类”属性设置为“ cell”。 请检查该财产。

您说您使用的是xib文件来实例化单元格,但是您在此行中注册的是类而不是xib

[self.table registerClass:[Cell class] forCellReuseIdentifier:@"cell" ];

尝试使用registerNib:forCellReuseIdentifier

暂无
暂无

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

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