acreichman
acreichman

Reputation: 23

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. 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.

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.

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

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

    // Configure the cell...

    return cell;
}

Upvotes: 2

Views: 2302

Answers (3)

Daniel Hall
Daniel Hall

Reputation: 13679

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.

Upvotes: 3

iOSNoob
iOSNoob

Reputation: 1420

Your cellForIndexViewPath should look like this, to create a simple custom cell with label,

- (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,

For storyboard:

enter image description here

Add "MyTableViewCell" instead of "SimpleTableCell" as shown in above screenshot.

Upvotes: 0

user3932021
user3932021

Reputation: 11

acreichman, add casting in cellForRow and put an NSLog in you cell's awakeFromNib to see if you get there. Let me know...

Upvotes: 0

Related Questions