10

我设置:

cell.selectionStyle = UITableViewCellSelectionStyleGray;

并使用代码突出显示一行:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection: 0];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone

即使我设置为灰色,突出显示颜色也始终为蓝色。如果我设置:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

它工作正常,没有亮点。但只是不适用于:

cell.selectionStyle = UITableViewCellSelectionStyleGray;

它只显示蓝色而不是灰色。任何想法?谢谢。

4

5 回答 5

30

实现如下: -

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {

     [cell setSelectionStyle:UITableViewCellSelectionStyleGray];       
}

或者

在自定义表格视图单元格(UITableViewCell 的子类)中将 's 颜色设置selectedBackgroundView为您想要的颜色:

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

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

//...
[cell setSelectedBackgroundView:selectedBackgroundView];
//...
于 2013-08-02T14:55:45.410 回答
15

请注意,在 iOS 7 中,使用

[单元格 setSelectionStyle:UITableViewCellSelectionStyleBlue];

不会像预期的那样工作,因为在 iOS 7 中它现在是灰色的,即使你传递了上面的常量。看:

https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/c/tdef/UITableViewCellSelectionStyle

于 2013-10-08T20:04:05.473 回答
5

只需在您的方法中添加它对我有用

- (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;
}

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

于 2014-08-08T06:55:52.527 回答
0

对“UITableViewCellSelectionStyleBlue”进行全局搜索,以确保您没有打错字。

于 2013-08-02T15:00:40.733 回答
0

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

于 2013-08-02T14:47:56.073 回答