我有一个表格单元格,显示用户图像,名称和一些文字。用户的图像是50x50,但我想要一个边框,所以我设置视图使图像居中,并将框架设置为52x52,然后将该视图的背景颜色设置为我的边框颜色。这表示图像周围有一个1像素的边框。
我还想在选择单元格时在单元格的右侧显示30像素宽的边框。我试图通过创建单元格框架大小的UIView,然后使用UIView添加我想要的宽度和背景颜色的子视图。然后我将该视图设置为单元格的selectedBackgroundView。
这里的问题是单元格的selectedBackgroundView被应用于单元格内所有视图的背景。因此,当我选择一个单元格时,图像“border”将设置为单元格选定的背景颜色,另外30px“边框”我将添加更改为该背景颜色。
我的cellForRowAtIndexPath中的代码:
cell = (UserCellView *) currentObject;
UIView *c = [[UIView alloc ] initWithFrame:CGRectMake(0, 0, 30, cell.frame.size.height)];
c.backgroundColor = [[UIColor alloc] initWithRed:64/255.0 green:64/255.0 blue:64/255.0 alpha:1.0];
UIView *v = [[UIView alloc ] initWithFrame:cell.frame];
v.backgroundColor = [[UIColor alloc] initWithRed:35/255.0 green:35/255.0 blue:35/255.0 alpha:1.0];
[v addSubview:c];
cell.selectedBackgroundView = v;
[c release];
[v release];
答案 0 :(得分:10)
我假设你没有实际测试了正在进行的分析,它“应用于单元格内所有视图的背景”。
我做了类似的事情:
@interface TestView : UIView {
}
@end
@implementation TestView
-(void)setBackgroundColor:(UIColor*)c {
// Breakpoint here.
NSLog("setBackgroundColor: %@",c);
[super setBackgroundColor:c];
}
@end
...
UIView * v = [[[UIView alloc] initWithFrame:(CGRect){{0,0},{20,20}}] autorelease];
v.backgroundColor = [UIColor magentaColor];
UIView * v2 = [[[TestView alloc] initWithFrame:(CGRect){{5,5},{10,10}}] autorelease];
v2.backgroundColor = [UIColor yellowColor];
[v addSubview:v2];
cell.selectedBackgroundView = v;
最终结果是,在选择视图时,-setBackgroundColor:
会调用-[UITableViewCell _setOpaque:forSubview:]
UIDeviceWhiteColorSpace 0 0
,例如[UIColor clearColor]
(即[UIColor clearColor]
)。
或者,换句话说,在选择单元格时,某些子视图的背景颜色设置为textLabel
,允许selectedBackgroundView显示。我认为会发生这种情况,因为常见的优化是为表格的背景颜色(例如白色)提供detailTextLabel
/ -setBackgroundColor:
所以它绘制得更快,但这意味着背景颜色必须选择单元格时重置。
最简单的解决方法是使用图像:如果有点乱,UIImageView中正确颜色的1 x 1像素图像将起作用。 (当我使用1像素高的UIViews绘制自定义分隔线时遇到了这个问题,所以我只是将分隔符包含在背景图像中。)
另一种解决方法是使用CALayer:将52x52子图层添加到UIImageView的图层,并设置子图层的背景颜色。我非常确定UITableViewCell只是走视图层次结构,所以它应该忽略自定义层。 (图层的最大缺点是它们不会自动调整大小,这使得它们不适合我的目的,并且意味着30px右边框不会自动调整大小。)
解决方法是对相关视图进行子类化,如果它等于[UIColor clearColor]
,则忽略{{1}}。
答案 1 :(得分:1)
一个简单但令人讨厌的解决方案是覆盖setSelected:animated:和setHighlighted:animated:with implementation重新设置你想要的各种背景。有点像:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
self.childView.backgroundColor = [UIColor blueColor]; // whichever you want
}
答案 2 :(得分:1)
首先将此添加到您的文件
#import <QuartzCore/QuartzCore.h>
然后将视图转换为带有...的图像
UIView *rowView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 1.0, 60.0)];
rowView.backgroundColor = [UIColor colorWithRed:35/255.0 green:35/255.0 blue:35/255.0 alpha:1.0];
UIGraphicsBeginImageContext(rowView.bounds.size);
[rowView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *yourImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
然后,不要将UIView添加到您的单元格,只需添加带有“yourImage”的UIImageView。
答案 3 :(得分:0)
如果受影响的视图可以是自定义子类,则一个简单的解决方案是覆盖-setBackgroundColor:
- (void)setBackgroundColor:(UIColor *)color
{
// Ignore requests and do nothing
}
因此UITableViewCell
尝试设置颜色将被忽略。自定义视图中真正想要设置背景颜色的代码需要调用super
:
- (void)setColor:(UIColor *)color
{
[super setBackgroundColor:color];
}
(或者可能直接向基础CALayer
发送消息)
答案 4 :(得分:-5)
您需要自定义单元格的contentView并处理委托tableView:cellForRowAtIndexPath