2011-07-08 39 views
5

我試圖更改自定義UITableViewCell的背景色在普通樣式的UITableView。改變在一個普通的自定義的UITableViewCell細胞的背景顏色風格的UITableView

我讀過其他類似的問題,我在我的委託方法:

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

我設置cell.backgroundColor = [UIColor...]。問題是這隻適用於分組風格UITableView

注:爲什麼我要在我的.xib文件編程,並沒有做到這一點的原因是因爲我想對每個小區不同的背景顏色。

+0

嘗試改變回地面顏色爲內容視圖。 cell.contentView.backGroundColor = [UIColor ...]; – Dinakar

+0

我沒有添加可可標籤? – petert

回答

8

也許你設置你的顏色,然後才能添加cell.contentView.backgroundColor = [UIColor clearColor]

的原因是backgroundview是在底部。 contentview位於頂部。

+0

這奏效了,謝謝你。任何想法之間的區別是什麼cell.backgroundColor和cell.contentView.backgroundColor? –

+1

該單元格有兩個圖層。一個是backgroundview,另一個是contentview。在backgroundview上有一個由蘋果公司提供的按鈕。如果你嘗試tableView.editing = YES,你可以看到刪除按鈕。它在backgroundview – Bonny

+0

對不起,仔細一看,這沒有奏效。我沒有設定cell.contentView.backgroundColor給我直接想要的顏色和沒有工作。所以請改變你的答案。謝謝 –

0
// set selection color 
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame]; 

myBackView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]; 

[cell.contentView addSubview:myBackView]; 

[myBackView release]; 

喜歡這種改變是你所需要

1

變化在以下委託方法顏色的每一個細胞:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (...){ 
     cell.backgroundColor = [UIColor blueColor]; 
    } else { 
     cell.backgroundColor = [UIColor whiteColor]; 
    } 
} 
相關問題