Raw*_*tar 2 cocoa-touch objective-c ios
我在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的框架尺寸似乎相当困难.任何帮助,将不胜感激.谢谢!
Stu*_* mc 12
您需要在Custom TableViewCell中使用它
- (void)layoutSubviews
{
[super layoutSubviews];
self.selectedBackgroundView.frame = CGRectMake(cell.frame.origin.x + 1.0f, cell.frame.origin.y, cell.frame.size.width - 22.0f, cell.frame.size.height);
}
Run Code Online (Sandbox Code Playgroud)
我不得不挠头.这有点愚蠢,因为这种或那种方式你需要两倍的框架.当然你可以使用define甚至属性UIView本身,但仍然......
我只能感谢Ortwin Gentz - 道具给他!