Aram Boyajyan
Aram Boyajyan

Reputation: 844

Loading custom XIB for UITableViewCell

I'm trying to use a custom XIB file for UITableViewCell, but no matter what I do the table cells are always empty - my XIB is simply not loaded.

There are no errors whatsoever.

Here's what I do:

  1. Create an empty application, add a storyboard, table view controller, add a new UITableViewController, hook it up with table and set some sample items.

  2. Create a controller as a subclass of UITableViewCell + check option to generate XIB.

  3. Create three sample labels and add them as IBOutlets.

  4. Load the XIB file and try to set it up as a cell.

Code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView.delegate = self;

    items = [[NSMutableArray alloc] init];
    for (int i = 0; i < 20; i++) {
        ItemObject *item = [ItemObject new];
        item.topLabel = [NSString stringWithFormat:@"Top %d", i];
        item.leftLabel = [NSString stringWithFormat:@"Left %d", i];
        item.rightLabel = [NSString stringWithFormat:@"Right %d", i];
        [items addObject:item];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return items.count;
}

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

    // Set up the cell.
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    if (!cell) {
        [tableView registerNib:[UINib nibWithNibName:@"CustomTableViewCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    }
    ItemObject *currentObject = [items objectAtIndex:indexPath.row];
    cell.topLabel.text   = currentObject.topLabel;
    cell.leftLabel.text  = currentObject.leftLabel;
    cell.rightLabel.text = currentObject.rightLabel;

    // General cell and table settings.
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [tableView setSeparatorColor:[UIColor clearColor]];

    return cell;
}

Where the error can be? Can someone point me in the right direction?

Thanks!

Upvotes: 3

Views: 7799

Answers (1)

spassas
spassas

Reputation: 4818

Make sure you've set the ItemCell identifier in your XIB and also that your table datasource is set. Then move your registerNib to viewDidLoad

-(void)viewDidLoad {
        //...
       self.tableview.dataSource = self;
       [self.tableView registerNib:[UINib nibWithNibName:@"CustomTableViewCell" bundle:nil] forCellReuseIdentifier:@"ItemCell"];

        }

and the in cellForRowAtIndexPath:

CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ItemCell" forIndexPath:indexPath];

and remove the if (!cell) block, it won't be used anyway.

Upvotes: 14

Related Questions