Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
所选tableviewCell的背景色设置框在iOS 7中工作正常,但在iOS 6中工作不正常_Ios_Objective C_Uitableview - Fatal编程技术网

所选tableviewCell的背景色设置框在iOS 7中工作正常,但在iOS 6中工作不正常

所选tableviewCell的背景色设置框在iOS 7中工作正常,但在iOS 6中工作不正常,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我想为所选tableviewCell的背景色设置边框。我正在使用以下代码 UIView * cellBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; cellBackgroundView.backgroundColor = [UIColor clearColor]; UIView * selectedCellColor = [[UIView alloc] initWithFrame:CGRectM

我想为所选tableviewCell的背景色设置边框。我正在使用以下代码

UIView * cellBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
cellBackgroundView.backgroundColor = [UIColor clearColor];
UIView * selectedCellColor = [[UIView alloc] initWithFrame:CGRectMake(5, 0, 300, 44)];
selectedCellColor.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:50.0/255.0 blue:50.0/255.0 alpha:1.0];
[cellBackgroundView addSubview:selectedCellColor];
cell.selectedBackgroundView = cellBackgroundView;
这在iOS7中运行良好。即使是Tableview选择也无法在iOS6中工作

以下代码在iOS6和ios7中都可以正常工作。但在这里,我无法为所选tableViewCell的背景色设置边框

UIView *selectedCellColor = [[UIView alloc] init];
[selectedCellColor setFrame:CGRectMake(0, 0, 320, 44)];
selectedCellColor.backgroundColor = [UIColor colorWithRed:50.0/255.0
                                                    green:50.0/255.0
                                                     blue:50.0/255.0
                                                    alpha:1.0];
cell.selectedBackgroundView=selectedCellColor;
编辑

请尝试以下方法:

[cell.contentView setBackgroundColor:[UIColor redColor]];
UIView *selectedBG = [[UIView alloc]initWithFrame:cell.contentView.frame];
[selectedBG setBackgroundColor:[UIColor greenColor]];
[cell.selectedBackgroundView addSubview:selectedBG];

cellForRowAtIndexPath:
中,调用
dequeueReusableCellWithIdentifier:forIndexPath:
(不仅仅是
dequeueReusableCellWithIdentifier:
。原因是现在您的单元格大小正确。不要硬编码单元格大小;将其作为您收到的单元格的
边界

现在画一个这样大小的UIImage,构建你的框架式绘图(图像本身一开始就很清晰)。将其放在UIImageView中,并将单元格的选定背景视图设置为该图像视图

这将在iOS 6和iOS 7中工作

示例(颜色和大小任意,仅用于说明):


当我点击单元格选定颜色(绿色)覆盖整个单元格时,使用上面的代码。但是我需要与x原点之间的一些间隙,如(10,0310,44)。在创建selectedBG
UIView*selectedBG=[[UIView alloc]initWithFrame:CGRectMake(10,0,cell.contentView.frame.size.width-10,cell.contentView.frame.size.height];
@user3373798您不负责所选背景视图的大小。您必须对实际大小做出响应。即使我放置cell.contentView.frame.size.width-100,总单元格背景颜色也会显示在单击时我尝试过这样的UIImageView*bGImage=[[UIImageView alloc]initWithFrame:cell.contentView.frame];bGImage.backgroundColor=[UIColor clearColor];UIView*selectedCellColor=[[UIView alloc]initWithFrame:CGRectMake(5,0,300,44)];selectedCellColor.backgroundColor=[UIColor WithRed:50.0/255.0绿色:50.0/255.0蓝色:50.0/255.0 alpha:1.0];[bImage addSubview:selectedCellColor];cell.selectedBackgroundView=bGImage;但是tableview选择的颜色在iOS6中没有变化。但是在IOS7中工作正常,但您没有按照我说的做(绘制图像)。我添加了代码来演示如何绘制图像。有关如何绘制的更多信息,请参阅我在此处的完整讨论:感谢您的详细回答。它的工作原理就像一个charmThanks!只是想澄清一下:问题在于,在iOS 6上,您无法控制框架如何操纵选定的背景视图。这就是为什么您使用图像视图的原因:您可以控制图像,因此现在您可以做您想做的事情。
UIGraphicsBeginImageContextWithOptions(cell.bounds.size, NO, 0);
// change this color as you like
[[UIColor redColor] setFill];
// fiddle with this size as you like
[[UIBezierPath bezierPathWithRect:CGRectMake(10,10,300,25)] fill];
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView* iv = [[UIImageView alloc] initWithImage:im];
cell.selectedBackgroundView = iv;