2012-03-15 59 views
2

當我點擊UITableViewCell中的單元格時,單元格默認爲藍色。 我該如何改變? 改變爲另一種顏色或我正在建造的這個樣本中,完全消除顏色。 我有一張桌子,只是一張桌子,信息在單元格內,按下時它不會導致任何地方。只要煩惱它會變成藍色,如果按下,因爲單元格中的文本更難以閱讀。TableView,如何更改單元格的默認藍色

+0

的可能重複(HTTP [如何改變一個UITableViewCell的藍色高亮顏色?]://計算器。com/questions/2553746 /如何更改藍色高亮顏色的可用視圖) – 2013-08-02 12:46:10

回答

8

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

方法添加以下代碼

cell.selectionStyle = UITableViewCellSelectionStyleNone; 
+0

或者更改界面構建器中的樣式以獲得相同的效果。 – jrturton 2012-03-15 07:47:47

+0

完美作品,謝謝 – 2012-03-15 09:05:58

1

更改屬性selectedBackgroundView是正確的,最簡單的方法。我使用下面的代碼來改變選擇的顏色:

//集選擇顏色

UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame]; 
myBackView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]; 
cell.selectedBackgroundView = myBackView; 
[myBackView release]; 

或u可以使用這個..

這裏是最有效的方式,我所遇到的解決這個問題,使用willDisplayCell委託方法(這利用cell.textLabel.text和/或cell.detailTextLabel.text當照顧白色文本標籤背景的爲好):

(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { ... } 

當此委託方法被稱爲小區的顏色是通過細胞而不是表視圖,控制爲當您使用:

(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { ... } 

所以細胞委託方法的主體內的以下代碼添加到或者只是使用函數調用來使表格的所有單元格具有相同的顏色。

if (indexPath.row % 2) 
{ 
     [cell setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]]; 
} 
else [cell setBackgroundColor:[UIColor clearColor]]; 
+0

謝謝,完美的,如果我想設置任何我想要的顏色。但是對於我的示例,第一個答案中的行已足夠,以消除顏色。 cell.selectionStyle = UITableViewCellSelectionStyleNone; – 2012-03-15 09:08:42

2

根據我改變uitableviewcell的選擇風格有以下幾種可能性。

  • 這些是可用的默認樣式。

    cell.selectionStyle = UITableViewCellSelectionStyleBlue; 
    
    cell.selectionStyle = UITableViewCellSelectionStyleGray; 
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    
  • 但是,如果你想改變風格,你也可以使用以下內容。

    cell.backgroundColor = [UIColor colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha]; 
    
    cell.backgroundColor = [UIColor colorWithPatternImage:(UIImage *)image]; 
    

使用第二種方法,您可以指定背景顏色或圖片。

對於選定的單元格樣式,您可以使用此選項。

cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:(UIImage *)image] autorelease]; 
+0

非常完美,非常感謝 – 2012-03-15 09:09:46

0

使用下面的代碼來對細胞選擇 刪除默認顏色:

cell.selectionStyle = UITableViewCellSelectionStyleNone; 
相關問題