1

I have a table. Within each cell of a table, I created a simple bar graph (which is just 2 UIViews with background filled).

I have set cell.selectedBackgroundView to an image.

When the cell is selected, it seems to cover up parts of the bar graph. Does anyone know why?

The red is the selected cell. The top and bottom are unselected cells: enter image description here

In the image, the grey bar, the brown bar, the 2 numbers (x.xx) and the semi-transparent line at the left, are all subviews of 1 UIView. The UIView is added to the cell. The line and the 2 numbers are still there, but the 2 bars are gone.

Here's some code:

Cell selected image set like this:

UIImage *selectedRowImage = [UIImage imageNamed:@"table-selectedcellbg-red-45px-stretch.png"];
cell.selectedBackgroundView = [[UIImageView alloc]initWithImage:[selectedRowImage resizableImageWithCapInsets:UIEdgeInsetsMake(selectedRowImage.size.height, selectedRowImage.size.width/2, selectedRowImage.size.height, selectedRowImage.size.width/2)]];
[cell sendSubviewToBack:cell.selectedBackgroundView]; // This didn't make a difference

Adding the view set like this:

UIView *graph = [barGraph getGraph];
UIView *graphView = [[UIView alloc]initWithFrame:CGRectMake(150, (tableView.rowHeight - graph.frame.size.height)/2, graph.frame.size.width, graph.frame.size.height)];
[graphView addSubview:graph];
[cell addSubview:graphView];

Any ideas?

Jason
  • 1,787
  • 4
  • 29
  • 46
  • What happens if you bring the `graphView` forward? – Rob Jun 19 '13 at 11:13
  • Added this just now, [cell bringSubviewToFront:graphView]; No difference. I don't think it's a matter of forward/back because the numbers and the line show up. If the UIView was behind, the two bars, the line and the numbers should all be hidden. – Jason Jun 19 '13 at 11:26
  • Yes, that's right. Silly me – Rob Jun 19 '13 at 11:33
  • Same problem and cannot find a solution. In my project, the background View and selectedBackgroundView are added during cellForRow. Then when selected nothing happens. If I add a selectedBackgroundView in setSelected it throws is over top all the other views. Proof with RevealApp. – jdog Feb 21 '14 at 04:04

1 Answers1

3

I think the answer can be found in the UITableViewClass Reference:

selectedBackgroundView

...

Discussion

The default is nil for cells in plain-style tables (UITableViewStylePlain) and non-nil for section-group tables (UITableViewStyleGrouped). UITableViewCell adds the value of this property as a subview only when the cell is selected. It adds the selected background view as a subview directly above the background view (backgroundView) if it is not nil, or behind all other views. Calling setSelected:animated: causes the selected background view to animate in and out with an alpha fade.

It seems as if selectedBackgroundView only works well if the corresponding backgroundView property of the cell is not nil.

Try adding a backgroundView and test again.

Hope that helps you out.


Edit:

Since this wasn't the solution, I tried to reproduce your problem, but with no success: the backgroundView stayed behind the QuestionMarkIcon (a view like your graphView)..

Code:

- (UITableViewCell*) tableView: (UITableView*) tableView cellForRowAtIndexPath: (NSIndexPath*) indexPath {
    //...

    UIImage* selectedRowImage = [UIImage imageNamed: @"strecheableImage.png"];
    cell.selectedBackgroundView = [[UIImageView alloc] initWithImage: [selectedRowImage resizableImageWithCapInsets: UIEdgeInsetsMake(selectedRowImage.size.height, selectedRowImage.size.width/2, selectedRowImage.size.height, selectedRowImage.size.width/2)]];

    UIView* graph = [[QuestionMarkIcon alloc] init];
    UIView* graphView = [[UIView alloc]initWithFrame: CGRectMake(150, (tableView.rowHeight - graph.frame.size.height)/2, graph.frame.size.width, graph.frame.size.height)];
    [graphView addSubview: graph];
    [cell addSubview: graphView];
    // ...
}

This is how my stretchableImage.png looks like: my <code>strecheableImage.png</code>

Cell 1 is selected but the QuestionMarkIcon (your graph) stays in front: Cell 1: selected - the QuestionMarkIcon (your graph) stays in front

Sorry I couldn't help.

anneblue
  • 706
  • 4
  • 21
  • Hi Anneblue, Thanks for this. I added a backgroundView, but still no luck. I tried a blank image (cell.backgroundView = [[UIImageView alloc]initWithImage:[[UIImage alloc]init]];) Could this be an Apple bug? – Jason Jun 19 '13 at 16:47
  • 4
    I think I figured this out. My bar graphs are UIViews with background set. I think when the cell is selected, all subviews are set to transparent. That's why the line and the textfields still show up. I can fix this by setting my UIViews to images rather than filled background color. I'm not sure why it does this, my best guess is for animation?? Don't know. – Jason Jun 20 '13 at 23:44
  • @Jason nailed it, when selected, the subviews of the cell will have their background become transparent. I just set the view's background to be an image instead of a background color using the extension found here: http://stackoverflow.com/questions/6496441/creating-a-uiimage-from-a-uicolor-to-use-as-a-background-image-for-uibutton – hobosf Jul 30 '15 at 21:03