我有一个表格单元格,显示用户图像,名称和一些文本.用户的图像是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)