简体   繁体   English

UITableViewCell自定义selectedBackgroundView问题

[英]UITableViewCell custom selectedBackgroundView issue

So I'm facing an issue with setting a custum selectedBackgroundView inside my UITableViewCell . 所以我遇到了在UITableViewCell设置custum selectedBackgroundView的问题。

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. 我的单元格有一个contentView ,它基本上是一个带有黑色背景和UIImageView作为子视图的UIView (frame = 0,0,80,70)。

The imageView's contentMode = UIViewContentModeScaleAspectFit; imageView的contentMode = UIViewContentModeScaleAspectFit;

This looks something like this: 这看起来像这样:

未选择的细胞

Now I set the selectedBackgroundView like this: 现在我像这样设置selectedBackgroundView:

    //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: 这导致如下所示:

在此输入图像描述

My question now is why the selectedBackgroundView hides the black part of the contentView ? 我现在的问题是为什么selectedBackgroundView隐藏了contentView的黑色部分?

I have already tried initializing my bgColorView with a frame starting at x = 80 , but this does not change anything. 我已经尝试使用从x = 80开始的帧初始化我的bgColorView ,但这不会改变任何东西。

Also I have tried to explictly set the backgroundColor of the imageView to black, same result. 此外,我曾试图显式设置backgroundColor的的imageView黑色,同样的结果。

What could cause this behaviour? 什么可能导致这种行为?

Following figure gives the idea of a cell structure. 下图给出了单元结构的概念。 The selected Background view will hide Content view 选定的背景视图将隐藏内容视图

在此输入图像描述

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

May be you can try overriding 也许你可以尝试覆盖

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

and

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

of UITableViewCell class to get the desired behavior. UITableViewCell类获取所需的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM