小编Raw*_*tar的帖子

UITableViewCell setSelectedBackground未正确显示帧

我在cellForRowAtIndexPath方法中为UITableViewCell正确设置selectedBackgroundView时遇到了一些困难.我尝试了一些代码变体......

CGRect customFrame = CGRectMake(cell.frame.origin.x + 1.0f, cell.frame.origin.y,
cell.frame.size.width - 22.0f, cell.frame.size.height);

UIView *cellBackground = [[[UIView alloc] initWithFrame:customFrame] autorelease];
cellBackground.backgroundColor = [UIColor colorWith8BitRed:0 green:0 blue:0 alpha:0.1f];
cell.selectedBackgroundView = cellBackground;
Run Code Online (Sandbox Code Playgroud)

使用此代码段,单元格显示正确的颜色更改,但框架尺寸不是我指定的(它们似乎是cell.frame的尺寸,即使我指定视图使用我的customFrame初始化)

我尝试了另一种方法:使用子子视图创建父视图.父视图的框架与单元格的尺寸相同,子视图的框架包含我的customFrame尺寸.当我将父视图设置为backgroundView时,将应用我的框架customFrame尺寸.但是,当我尝试将父视图设置为selectedBackgroundView时,我在选择单元格时看不到任何视图.

CGRect customFrame = CGRectMake(cell.frame.origin.x + 1.0f, cell.frame.origin.y, 
cell.frame.size.width - 22.0f, cell.frame.size.height);

UIView *parentView = [[[UIView alloc] initWithFrame:cell.frame] autorelease]; 
UIView *childView = [[[UIView alloc] initWithFrame: customFrame] autorelease];

[parentView addSubview:childView];
childView.backgroundColor = [UIColor colorWith8BitRed:0 green:0 blue:0 alpha:0.1f];
cell.selectedBackgroundView = parentView;
Run Code Online (Sandbox Code Playgroud)

自定义selectedBackgroundView的框架尺寸似乎相当困难.任何帮助,将不胜感激.谢谢!

cocoa-touch objective-c ios

2
推荐指数
1
解决办法
1064
查看次数

标签 统计

cocoa-touch ×1

ios ×1

objective-c ×1