UITableViewCell custom selectedBackgroundView issue

13,085

Following figure gives the idea of a cell structure. The selected Background view will hide Content view

enter image description here

So you can give a try by setting background color of cell as black. Your custom cell looks something like this

    self.backgroundColor = [UIColor blackColor]; // Gives black background for you


    // Set selected background view
    UIView *backgroundView = [[UIView alloc]initWithFrame:self.bounds];
    backgroundView.layer.borderColor = [[UIColor colorWithRed:0.529 green:0.808 blue:0.922 alpha:1]CGColor];
    backgroundView.layer.borderWidth = 10.0f;
    self.selectedBackgroundView = backgroundView;
    [backgroundView release];

    // Set the content view
    CGRect frame  = CGRectMake(self.bounds.origin.x+5, self.bounds.origin.y+5, self.bounds.size.width-10, self.bounds.size.height-10);
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
    self.imageView = imageView;
    [imageView release];
    self.imageView.contentMode = UIViewContentModeScaleAspectFill ;
    self.imageView.clipsToBounds = YES;
    [self.contentView addSubview:self.imageView];
Share:
13,085
pmk
Author by

pmk

PHP iOS

Updated on June 19, 2022

Comments

  • pmk
    pmk almost 2 years

    So I'm facing an issue with setting a custum selectedBackgroundView inside my UITableViewCell.

    My cell has a contentView that basically is a UIView (frame = 0,0,80,70) with a black background and an UIImageView as a subview.

    The imageView's contentMode = UIViewContentModeScaleAspectFit;

    This looks something like this:

    unselected cells

    Now I set the selectedBackgroundView like this:

        //set the custom selected color
        UIView *bgColorView                 = [[UIView alloc] init];
        bgColorView.backgroundColor         = MY_TINT_COLOR;
        CGColorRef darkColor                = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha: 0.25].CGColor;
        CGColorRef lightColor               = [self.view.backgroundColor colorWithAlphaComponent:0.0].CGColor;
        //setting some gradients here
        //should not be relevant for the question
        [cell setSelectedBackgroundView:bgColorView];
    

    This results in something like this:

    enter image description here

    My question now is why the selectedBackgroundView hides the black part of the contentView?

    I have already tried initializing my bgColorView with a frame starting at x = 80, but this does not change anything.

    Also I have tried to explictly set the backgroundColor of the imageView to black, same result.

    What could cause this behaviour?

  • pmk
    pmk about 11 years
    Hm I was hoping not to need to subclass UITableViewCell. Anyway I will give it a try and tell you a if it was successfull. Thanks so far!
  • pmk
    pmk about 11 years
    Thank you for your helpfull answer! I did not know about the cell's view hierarchy. Setting the contentView to a UIImageView solved the problem :)
  • Mihai Erős
    Mihai Erős almost 6 years
    Thank you for your answer, helped me alot!
  • GeneCode
    GeneCode over 4 years
    I dont get it. From the diagram, contentview is most top, so why would setting selectedbackgroundview cover the contentview?