Custom Selective Selection UITableViewCellBackgroundView

So, I had a problem setting custum selectedBackgroundView inside my UITableViewCell .

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

contentMode = UIViewContentModeScaleAspectFit;

It looks something like this:

unselected cells

Now I set selectedBackgroundView as follows:

  //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]; 

The result is approximately the following:

enter image description here

Now my question is: why does selectedBackgroundView hide the black part of the contentView ?

I already tried to initialize my bgColorView frame starting with x = 80 , but that does not change anything.

I also tried to explicitly set backgroundColor of imageView to black, the same result.

What can cause this behavior?

+4
source share
2 answers

The following figure gives an idea of ​​the structure of the cell. The selected background view will hide the content view.

enter image description here

So, you can try setting the background color of the cell to 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]; 
+22
source

Perhaps you can try redefining

 - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated 

and

 - (void)setSelected:(BOOL)selected animated:(BOOL)animated 

the UITableViewCell class to get the desired behavior.

-1
source

All Articles