【问题标题】:UITableViewCell custom selectedBackgroundView issueUITableViewCell 自定义 selectedBackgroundView 问题
【发布时间】:2013-02-26 14:06:15
【问题描述】:

所以我遇到了在我的UITableViewCell 中设置 custum selectedBackgroundView 的问题。

我的单元格有一个contentView,它基本上是一个黑色背景的UIView(帧= 0,0,80,70)和一个UIImageView 作为子视图。

imageView 的contentMode = UIViewContentModeScaleAspectFit;

这看起来像这样:

现在我像这样设置 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];

结果如下:

我现在的问题是为什么selectedBackgroundView 隐藏了contentView 的黑色部分?

我已经尝试使用从x = 80 开始的帧来初始化我的bgColorView,但这并没有改变任何东西。

我还尝试将imageViewbackgroundColor 显式设置为黑色,结果相同。

什么可能导致这种行为?

【问题讨论】:

    标签: iphone ios objective-c uitableview uiimageview


    【解决方案1】:

    也许你可以尝试覆盖

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

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

    UITableViewCell 类以获得所需的行为。

    【讨论】:

    • 嗯,我希望不需要继承 UITableViewCell。无论如何,我会试一试并告诉你它是否成功。到目前为止谢谢!
    【解决方案2】:

    下图给出了细胞结构的概念。选定的背景视图将隐藏内容视图

    因此,您可以尝试将单元格的背景颜色设置为黑色。 您的自定义单元格看起来像这样

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

    【讨论】:

    • 感谢您的帮助!我不知道单元格的视图层次结构。将contentView 设置为UIImageView 解决了问题:)
    • 感谢您的回答,对我帮助很大!
    • 我不明白。从图中,contentview 是最靠前的,为什么设置 selectedbackgroundview 会覆盖 contentview?
    猜你喜欢
    • 2023-03-05
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多