BON
BON

Reputation: 443

Custom tableviewcell not displaying

I have created a custom tableviewcell. The class has 3 labels. Using a master view controller template to get started, I changed the default tableviewcell in my storyboard to reference my new custom cell, I also changed the type to custom and the identifer to 'CustomTableCell'. I have also modified my cellForRowAtIndexPath method to the following...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"CustomTableCell";

    CustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell)
    {
        cell = [[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    Item *currentItem = _objects[indexPath.row];
    cell.nameLabel.text = [currentItem name];
    cell.vegLabel.text = @"V";
    return cell;
}

CUSTOM CELL HEADER FILE

#import <UIKit/UIKit.h>

@interface CustomTableCell : UITableViewCell

@property (nonatomic, weak) IBOutlet UILabel *nameLabel;
@property (nonatomic, weak) IBOutlet UILabel *vegLabel;
@property (nonatomic, weak) IBOutlet UILabel *priceLabel;

@end

Eveything seems to be connected properly in my storyboard. When I debug I can see that the cell has the properties of my custom cell. Yet when I run the application each row in blank. The tableviewcell is using the correct identifier in the story board. I just can't see what i'm missing. Any help would be appreciated. Thanks.

Identifier in storyboard Connections in storyboard

Upvotes: 0

Views: 388

Answers (1)

Dharmbir Singh
Dharmbir Singh

Reputation: 17535

You are not loading custom cell from mainbundle. So you need to load it.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"CustomTableCell";

    CustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    // Add this line in your code
    cell = [[[NSBundle mainBundle]loadNibNamed:@"CustomTableCell" owner:self options:nil]objectAtIndex:0]; 

    if (!cell)
    {
        cell = [[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    Item *currentItem = _objects[indexPath.row];
    cell.nameLabel.text = [currentItem name];
    cell.vegLabel.text = @"V";
    return cell;
}

Upvotes: 1

Related Questions