故事板中的空白原型单元格

时间:2015-10-02 11:53:30

标签: ios objective-c uitableview

我有一个连接到表视图的tableviewcontroller。我想在表视图中有一个自定义单元格类型。自定义单元格应该有三个UILabels和一个UIImageView

我在故事板中设计了tableview中的自定义单元格(使用原型单元格)。我创建了一个UITableViewCell的子类,并将原型单元格链接到这个类。我还将单元格的重用标识符设置为" ItemCell"。

UITableView我有一个添加按钮。当我按下此按钮时,应将新单元格(自定义单元格)添加到tableview中。他们是(我可以通过我可以选择它们的事实来判断)除了它们是空白的(尽管标签应该显示一些东西)。标签作为UITableViewCell属性连接到IBOutlet子类,并且具有灰色背景,因此我可以看到它的框架,但我看到的只是一个白色的行。

出了什么问题?

这是viewDidLoad中的UITableViewController方法。

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[ItemCell class] forCellReuseIdentifier:@"ItemCell"];
    self.navigationItem.title = @"Home";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                                                           target:self
                                                                                           action:@selector(addNewItem:)];
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
}

这是cellForRowAtIndexPath:方法的代码。

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

    cell.nameLabel.text = @"hello";

    return cell;
}

1 个答案:

答案 0 :(得分:1)

如果您正在使用原型单元格,则在故事板中指定重用标识符,并从中解压缩单元格。通过调用此代码:

[self.tableView registerClass:[ItemCell class] forCellReuseIdentifier:@"ItemCell"];

您正在删除该注册并将其替换为ItemCell类的简单空实例,因此不会有子视图和没有填充的插件。

删除要修复的代码行。