2010-01-05 31 views
2

我想創建一張圖片作爲背景。 要做到這一點,我開始了背景:UITableViewCellStyleSubtitle標籤的BackgroundColor?

self.view.backgroundColor = [UIColor groupTableViewBackgroundColor]; 

這導致出現在tablecells以及背景圖像。這不是我想要的,所以我試圖設置單元格的背景顏色:

cell.backgroundColor = [UIColor whiteColor]; 

這完全沒有效果!!!嗯奇怪。 所以我試過這個:

UIView *backgroundView = [[UIView alloc] init]; 
backgroundView.backgroundColor = [UIColor whiteColor]; 
cell.backgroundView = backgroundView; 
[backgroundView release]; 

這個工程差不多。剩下的唯一問題是detailTextLabel的textLabel &仍然顯示它們背後的背景。 將這些標籤上的backgroundColor設置爲白色也不會執行任何操作。

我該如何繼續?我哪裏出錯了?我正在嘗試實現像worldClock應用程序一樣的tableView。

回答

10

要更改表格視圖單元格的背景顏色,你需要將其設置爲tableView:willDisplayCell:forRowAtIndexPath:而非tableView:cellForRowAtIndexPath: 否則不會有任何影響,例如:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    cell.backgroundColor = [UIColor whiteColor]; 
} 
+0

謝謝!正是我需要的。 –

+0

我仍然不完全明白爲什麼使用cellForRowAtIndexPath:不起作用,但你已經解決了我的問題。謝謝! – radven