Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.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
Ios 无法更改UITableView蓝色高亮显示颜色_Ios_Objective C - Fatal编程技术网

Ios 无法更改UITableView蓝色高亮显示颜色

Ios 无法更改UITableView蓝色高亮显示颜色,ios,objective-c,Ios,Objective C,我设定: 并使用代码高亮显示一行: cell.selectionStyle = UITableViewCellSelectionStyleGray; 高光颜色始终为蓝色,即使我设置为灰色。如果我设置: NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection: 0]; [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UIT

我设定:

并使用代码高亮显示一行:

cell.selectionStyle = UITableViewCellSelectionStyleGray;
高光颜色始终为蓝色,即使我设置为灰色。如果我设置:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection: 0];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone
它工作良好,没有突出显示。但只是不适用于:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

它只显示蓝色而不是灰色。有什么想法吗?谢谢。

请确保在InterfaceBuilder或
cellForRowAtIndexPath:
方法中进行此类配置。

按如下方式实施:-

cell.selectionStyle = UITableViewCellSelectionStyleGray;

将所选背景视图的颜色设置为自定义tableview单元格(UITableViewCell的子类)中所需的颜色:

或者您可以在
-tableView:cellforrowatinexpath:
方法中配置它:

UIView * selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame];
[selectedBackgroundView setBackgroundColor:[UIColor redColor]]; // set color here
[self setSelectedBackgroundView:selectedBackgroundView];

对“UITableViewCellSelectionStyleBlue”进行全局搜索,以确保您没有输入错误。

请注意,在iOS 7中,使用

[单元格设置选择样式:UITableViewCellSelectionStyle蓝色]

将不会像预期的那样工作,因为在iOS 7中,这现在是灰色的,即使你通过上面的常数。见:


只需将此添加到您的方法中,这对我很有用

//...
[cell setSelectedBackgroundView:selectedBackgroundView];
//...

如果您有任何问题,请随时提问

请发布代码,其中包含设置选择颜色和执行选择的完整方法体。您不是偶然在iOS 7中尝试此操作的,是吗?是的。我最初是在IB学习的,没有工作。然后,我在cellForRowAtIndexPath中做了,但仍然没有工作。谢谢。成功了。我使用自定义单元格。看起来像setSelectionStyle:UITableViewCellSelectionStyleGray不适用于自定义单元格。此外,您不需要设置边框,因此设置选择颜色的最短方法是在
tableView:cellForRowAtIndexPath:
cell.selectedBackgroundView=[[UIView alloc]init]中的这两行;cell.selectedBackgroundView.backgroundColor=[UIColor redColor](如果不使用ARC,则向init添加自动释放)。这是一个很好的答案。它给你正确的蓝色,如果这是你正在寻找的颜色。在我的例子中,当我指定默认的蓝色(UITableViewCellSelectionStyleBlue)时,背景是灰色的。苹果已经不推荐使用蓝色样式,但没有使用“不推荐”一词。“蓝色=选定时单元格具有默认背景色”
//...
[cell setSelectedBackgroundView:selectedBackgroundView];
//...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
....
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor colorWithRed:(76.0/255.0) green:(161.0/255.0) blue:(255.0/255.0) alpha:1.0]; // perfect color suggested by @mohamadHafez
bgColorView.layer.masksToBounds = YES;
cell.selectedBackgroundView = bgColorView;
....
return cell;
}