2013-08-02 70 views
9

我設置:無法更改UITableView的藍色高亮顏色

cell.selectionStyle = UITableViewCellSelectionStyleGray; 

,並使用代碼高亮顯示一行:

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

高亮顏色總是很藍,即使我設置爲灰色。如果我設置:

cell.selectionStyle = UITableViewCellSelectionStyleNone; 

它工作正常,沒有高光。但只是不適用:

cell.selectionStyle = UITableViewCellSelectionStyleGray; 

它只是顯示藍色而不是灰色。任何想法?謝謝。

+0

請張貼代碼,包括完整的方法體您在哪裏設置選擇顏色並執行選擇。 –

+0

你不是偶然在iOS 7中嘗試這個,是嗎? –

回答

28

如下實現: -

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

    [cell setSelectionStyle:UITableViewCellSelectionStyleGray];  
} 

OR

設置selectedBackgroundView的顏色,只要你想在您的自定義的tableview細胞是什麼(是的UITableViewCell的子類):

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

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

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

謝謝。有效。我使用自定義單元格。看起來像setSelectionStyle:UITableViewCellSelectionStyleGray不適用於自定義單元格。 – user2543991

+6

此外,您不需要設置框架,所以設置選擇顏色的最短途徑是'tableView:cellForRowAtIndexPath:'中的這兩行:cell.selectedBackgroundView = [[UIView alloc] init]; cell.selectedBackgroundView.backgroundColor = [UIColor redColor];' (如果您不使用ARC,請將autorelease添加到init。) –

0

請確保在InterfaceBuilder或cellForRowAtIndexPath:方法中執行此類配置。

+0

是的。我第一次在IB工作,但沒有工作。然後,我在cellForRowAtIndexPath中完成了,但仍然無法工作。 – user2543991

0

對「UITableViewCellSelectionStyleBlue」進行全局搜索以確保您沒有輸入錯字。

4

就在你的方法添加這個工作對我來說

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

,如果您有任何問題隨時問