1
votes

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?

3
Don't you also have to change the owner in IB to your view controller? - onnoweb
Ignore my answer, my phone broke up the post. - borrrden
First guess would be that you're loading UIView from a xib and you should set it as cell.contentView. Try setting: cell.contentView = (UIView *)[topLevelObjects objectAtIndex:0]; instead of cell = [topLevelObjects objectAtIndex:0]; - Rok Jarc
rokjarc: I get assignment to read-only property error with that line - sayguh
onnoweb: I don't think so? I'm looking at another example I have with this working and I don't see any references to file owner or anything in the XIB - sayguh

3 Answers

1
votes

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];
}
1
votes

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

0
votes

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"];