0

My simple custom UITableViewCell subclass does not show its content when selected. When I tap one of the cells, the green view disappears and I only see the cell's red background color. When I select another cell, the content of the previously selected one appear again. Any ideas?

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.backgroundColor = [UIColor redColor];

        UIView* someView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
        someView.backgroundColor = [UIColor greenColor];

        [self.contentView addSubview:someView];
    }

    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

I registered this with in my view controller's `viewDidLoad':

[self.tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"Identifier"];

And here is how I create them:

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

    if (cell == nil)
    {
        cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:@"Identifier"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    return cell;
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
meaning-matters
  • 21,929
  • 10
  • 82
  • 142

1 Answers1

2

The background of your cell should be set in the backgroundView and selectedBackgroundView properties, not added as a subview of contentView. Only the content (text, images, etc.) should be in contentView.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287