Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell custom selectedBackgroundView issue

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?

like image 237
pmk Avatar asked Mar 11 '13 11:03

pmk


1 Answers

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];
like image 142
Anil Varghese Avatar answered Sep 20 '22 13:09

Anil Varghese