iphone - UITableViewCell自定义selectedBackgroundView问题

标签 iphone ios objective-c uitableview uiimageview

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

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

imageView 的 contentMode = UIViewContentModeScaleAspectFit;

这看起来像这样:

unselected cells

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

结果是这样的:

enter image description here

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

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

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

什么会导致这种行为?

最佳答案

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

enter image description here

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

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

关于iphone - UITableViewCell自定义selectedBackgroundView问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15337876/

相关文章:

ios - 使用 Swift 编写 iOS 应用程序时在 iPhone 中获取奇怪的磁力计数据

iphone - 如何在不使用TexturePacker的情况下在Cocos2D中添加动画图像?

ios - iOS drawInRect内存泄漏-ARC

objective-c - 只读 socket ?

iphone - UIImage/UIView 不能正确调整到 UIInterfaceLandscape View

IOS/Iphone : how to open a pdf link without . iphone 浏览器中的 pdf 扩展

ios - 在 UIImagePickerController SourceType Camera 之后立即调整图像大小

iphone - 为什么我们在 Objective-C 或 C 语言中使用双指针和三指针?

ios - iOS获取键盘语言

iphone - 通过 wifi 在 iOS 设备之间同步核心数据?