sayguh
sayguh

Reputation: 2550

Why is my custom UITableViewCell not showing?

I've done custom UITableViewCells before without issue.. but I can't figure out what is going on with my current project.

Here's what I've done...

  1. Create CustomCell.h (subclassing UITableViewCell)
  2. Create an empty XIB and drag in UITableViewCell. Set background to black.
  3. Change class of UITableViewCell in Interface Builder to "CustomCell"
  4. Import CustomCell.h in my DetailViewController
  5. Modify tableView:cellForRowAtIndexPath:
static NSString *CellIdentifier = @"Cell";

CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    NSLog(@"DO I GET HERE?");
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
    cell = [topLevelObjects objectAtIndex:0];
}

I would expect my tableview cells to show as black? But they are still showing as white... any ideas? Thanks!

Update: Ok, turns out, it is loading the custom cell... I added a UILabel with some white text. I couldn't see it, but when I highlighted the cell I could see the text was there. So now the question becomes, why is the cell ignoring the black background I have set for the cell?

Upvotes: 1

Views: 4758

Answers (3)

Nick
Nick

Reputation: 19664

You have to register your custom cell with the tableview

This needs to happen before that delegate is called:

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

Upvotes: 0

sayguh
sayguh

Reputation: 2550

Seems to ignore the background colour I've set, so I just add a UIView to it with a background colour and that seems to work..

Upvotes: 1

Damo
Damo

Reputation: 12890

EDIT: As for why it's not black - I expect there is something obscuring your black - the likeliest candidate for this is the label background being white and not clear.

As well as point 3.

The attributes inspector (the 4th tab) needs to have the reuse identifier set to the identifier you are going to reuse (you use @"cell" in your question). I would try and use something a bit more specific - after all in some apps you might have many types of custom cells.

I think you also need to cast the topLevelObjects to (CustomCell*) thus

if (cell == nil) {
    NSLog(@"DO I GET HERE?");
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
    cell = (CustomCell*)[topLevelObjects objectAtIndex:0];
}

Upvotes: 1

Related Questions