UITableView接收自定义UITableViewCell但没有显示任何内容

时间:2014-06-14 13:31:23

标签: ios objective-c uitableview custom-cell

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

Cell.m

#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类中包含名为table的UITableView。它也是Delegate和Datasource。两者都在故事板中设置。它的代码如下:

@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

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

我在这样的问题上读了几篇文章,但没有人帮助过我。不同的教程说它应该像这样工作,但它没有。我认为可能有一个愚蠢的错误。谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

我猜你忘记在* .xib文件中设置自定义类名。 在* .xib属性中将Custom Class属性设置为“cell”。请检查该财产。

答案 1 :(得分:0)

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

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

尝试使用registerNib:forCellReuseIdentifier:改为

相关问题