所以我遇到了在我的 UITableViewCell
中设置自定义 selectedBackgroundView 的问题。
我的单元格有一个 contentView
,它基本上是一个 UIView
(frame = 0,0,80,70),带有黑色背景和一个 UIImageView
作为 subview 。
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
,但这并没有改变任何东西。
我还尝试将 imageView
的 backgroundColor
显式设置为黑色,结果相同。
什么会导致这种行为?
最佳答案
下图给出了细胞结构的概念。选定的背景 View 将隐藏内容 View
因此您可以尝试将单元格的背景颜色设置为黑色。 您的自定义单元格看起来像这样
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/