Alex Craciun
Alex Craciun

Reputation: 473

Blank prototype cells in Storyboards

I have a tableviewcontroller connected to a table view. I want to have a custom cell type in the table view. The custom cell should have three UILabels and a UIImageView.

I have designed the custom cell in the tableview (using a prototype cell) in the storyboard. I have created a subclass of UITableViewCell and have linked the prototype cell to this class. I have also set the reuse identifier of the cell to be "ItemCell".

In the UITableView I have an add button. When I press this button new cells (custom cells) should be added to the tableview. They are (I can tell by the fact that I can select them) except they are blank (although the label should display something). The label is connected to the UITableViewCell subclass as a IBOutlet property and has a grey background so I can see its frame but all I see is a white row.

What's wrong?

This is the viewDidLoad method from the 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;
}

This is the code from the cellForRowAtIndexPath: method.

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

    cell.nameLabel.text = @"hello";

    return cell;
}

Upvotes: 0

Views: 438

Answers (1)

Wain
Wain

Reputation: 119021

If you're using prototype cells then you specify the reuse identifier in the storyboard and cells are unpacked from there. By calling this code:

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

you are removing that registration and replacing it with a simple empty instance of the ItemCell class, so there will be no subviews and no populated outlets.

Remove that line of code to fix.

Upvotes: 2

Related Questions