UITableView cell: selectedBackgroundView without showing backgroundView

Go To StackoverFlow.com


I want to setup 2 completely different look(and content) of a cell. (one selected, and one not selected).

This code works, BUT, because the image for the selected cell was 50% transparent, I can see the BackgroundView on bottom.

In clear, selectedBackgroundView offer a nice way to populate a cell with element only visible when selected. Is there another location than BackgroundView to populate cell, a location how will not appear on selectedBackgroundView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  NSString *cellIdentifier = @"hello";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

    UIImage *image = [UIImage imageNamed:@"listview_btn.png"];
    UIImage *imageThumb = [UIImage imageNamed:@"01_thumb.jpg"];
    UIImageView* imageView = [[UIImageView alloc] initWithImage:imageThumb];
    [imageView setFrame:CGRectMake(0, 0, 50, 67)];
    [cell setBackgroundView:[[UIImageView alloc]initWithImage:image] ];
    [cell.backgroundView addSubview:imageView];

    UIImage *imageSel = [UIImage imageNamed:@"listview_btn_on.png"];
    UIImage *imageThumbSel = [UIImage imageNamed:@"01_thumb_Sel.jpg"];
    UIImageView* imageViewSel = [[UIImageView alloc] initWithImage:imageThumbSel];
    [imageViewSel setFrame:CGRectMake(110, 0, 50, 67)];
    [cell setSelectedBackgroundView:[[UIImageView alloc]initWithImage:imageSel] ];
    [cell.selectedBackgroundView addSubview:imageViewSel];
2012-04-04 00:58
by Franck
It took me one click to fix your code formatting. Maybe you should do that click the next time. To do so, select all the code you have inserted into the question and press the {} button. It will add proper markdown indention so your code is readable and gets the nice color scheme :- - Matthias Bauch 2012-04-04 02:55
And of course it is Xcode not xCode. Xcode is not an iPhone. Xcode is just an IDE, it's a tool to write your code. If your question is not about the IDE itself (e.g. "How do I turn on line numbers in Xcode?", please don't use the tag - Matthias Bauch 2012-04-04 02:59
No answer for this question? (and thanks for the corrections Matthias - Franck 2012-04-07 12:57


Make the selected background view opaque. Since it is always inserted above the background view, you can composite them in Photoshop or another graphics editor and use the result as the selected background view.

If you cannot afford this approach (I still highly recommend it), you can subclass UITableViewCell and override both setHighlighted:animated and setSelected:animated to set the background view property to nil when selected/highlighted and restore it when deselected/unhighlighted. However it is a much more involved approach, IMHO.

2012-04-28 05:00
by Costique