Jas*_*onB 4 iphone cocoa-touch uikit ipad
我有一个表格单元格,显示用户图像,名称和一些文本.用户的图像是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];
Run Code Online (Sandbox Code Playgroud)
tc.*_*tc. 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;
Run Code Online (Sandbox Code Playgroud)
最终结果是-setBackgroundColor:从-[UITableViewCell _setOpaque:forSubview:]选择视图时调用,例如UIDeviceWhiteColorSpace 0 0(ie [UIColor clearColor]).
或者,换句话说,在[UIColor clearColor]选择单元格时,某些子视图的背景颜色被设置为允许selectedBackgroundView显示.我想这是因为一个共同的优化就是给textLabel/ detailTextLabel表的背景颜色(如白色),所以它吸引更快,但这意味着背景色在选中单元格时被重置.
最简单的解决方法是使用图像:如果有点乱,UIImageView中正确颜色的1×1像素图像将起作用.(当我使用1像素高的UIViews绘制自定义分隔线时,我遇到了这个问题,所以我只是将分隔符包含在背景图像中.)
另一种方法是使用CALayer:将一个52x52子图层添加到UIImageView的图层,并设置子图层的背景颜色.我非常确定UITableViewCell只是走视图层次结构,所以它应该忽略自定义层.(图层的一大缺点是它们没有自动调整大小,这使得它们不适合我的目的,并且意味着30px右边框不会自动调整大小.)
解决方法是对相关视图进行子类化,并忽略-setBackgroundColor:它是否相等[UIColor clearColor].
| 归档时间: |
|
| 查看次数: |
5402 次 |
| 最近记录: |