2008-11-11 125 views
183

我想定製我的UITableView中所有UITableViewCell的背景(也可能是邊框)。到目前爲止,我還沒有能夠自定義這個東西,所以我有一堆白色的背景單元,這是默認的。如何自定義UITableViewCell的背景顏色?

有沒有辦法用iPhone SDK做到這一點?

回答

186

您需要將單元格的contentView的backgroundColor設置爲您的顏色。如果您使用附件(例如公開箭頭等),它們將顯示爲白色,因此您可能需要滾動這些附件的自定義版本。

+103

例如myCell.contentView.backgroundColor = [UIColor greenColor]; – JoBu1324 2009-07-14 20:10:48

+1

vlado.grigorov的答案更加靈活 - 請參閱William Denniss的答案 – JoBu1324 2009-07-14 20:35:43

+2

有關如何推出自己的`UITableViewCellAccessoryCheckmark`的任何鏈接,例如?謝謝。 – 2011-09-09 00:55:12

3

自定義表格視圖單元格的背景最終變成和「全部或全部」方法。以一種看起來不奇怪的方式改變用於內容單元背景的顏色或圖像是非常困難的。

原因是單元實際跨越了視圖的寬度。圓角只是其繪圖風格的一部分,內容視圖位於此區域。

如果更改內容單元格的顏色,最終會在角落處顯示白色的點。如果更改整個單元格的顏色,則會有一個跨越視圖寬度的顏色塊。

你絕對可以自定義一個單元格,但它起初並不像你想象的那麼容易。

+1

對於分組表格中的單元格,這肯定是正確的,但普通表格沒有太多需要擔心的事情。沒有配件的裝飾細胞應該看起來不錯。 – 2008-11-11 23:54:35

+0

本 - 好點。我主要使用分組表,但正如你提到普通表不會遇到任何這些問題。 – 2008-11-12 18:45:52

57

迄今爲止發現的最佳方法是設置單元格的背景視圖並清除單元格子視圖的背景。當然,這隻適合索引風格的桌子,不管有沒有配件。

這裏是小區的背景喘着氣黃色的樣本:

UIView* backgroundView = [ [ [ UIView alloc ] initWithFrame:CGRectZero ] autorelease ]; 
backgroundView.backgroundColor = [ UIColor yellowColor ]; 
cell.backgroundView = backgroundView; 
for (UIView* view in cell.contentView.subviews) 
{ 
    view.backgroundColor = [ UIColor clearColor ]; 
} 
+1

如果您這樣做,請確保將不透明屬性設置爲NO。 – 2009-03-08 06:03:02

4

vlado.grigorov有一些很好的建議 - 最好的辦法是創建一個backgroundView,並給予一個顏色,設置一切到clearColor。此外,我認爲這是正確清除顏色的唯一方法(嘗試他的示例 - 但設置'clearColor'而不是'yellowColor'),這正是我想要做的。

203

下面是我遇到的最有效的方法來解決這個問題,使用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]]; 

該解決方案在我的環境下運作良好...

19

簡單地說這在你的UITableView委託類文件(.M):

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UIColor *color = ((indexPath.row % 2) == 0) ? [UIColor colorWithRed:255.0/255 green:255.0/255 blue:145.0/255 alpha:1] : [UIColor clearColor]; 
    cell.backgroundColor = color; 
} 
104

這是非常簡單的,因爲OS 3.0只需在willDisplayCell方法中設置單元格的背景顏色即可。 您不能在cellForRowAtIndexPath中設置顏色。

這同時適用於平原和分組方式:

代碼:

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

PS:這裏willDisplayCell文件摘錄:

「表視圖將此消息發送到 其代表就在它使用單元格 繪製一行之前,從而允許 委託自定義單元格對象顯示之前,請選擇 。此方法 爲代表提供了一個機會,即 覆蓋基於狀態的屬性設置 較早的表視圖,如 選擇和背景顏色。後 委託返回時,表視圖 集僅α和幀 屬性,然後僅當 動畫行,因爲它們在或 出來。」

滑動我發現在this post from colionel此信息。感謝他!

7

我西巴同意, 我試圖設置在rowForIndexPath委託方法我交替行的顏色,但漸漸3.2和4.2之間不一致的結果。對我來說,下面偉大的工作。

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

    if ((indexPath.row % 2) == 1) { 
     cell.backgroundColor = UIColorFromRGB(0xEDEDED); 
     cell.textLabel.backgroundColor = UIColorFromRGB(0xEDEDED); 
     cell.selectionStyle = UITableViewCellSelectionStyleGray; 
    } 
    else 
    { 
     cell.backgroundColor = [UIColor whiteColor]; 
     cell.selectionStyle = UITableViewCellSelectionStyleGray; 
    } 

} 
2

我的解決方案是將下面的代碼添加到的cellForRowAtIndexPath事件:

在任何情況下
UIView *solidColor = [cell viewWithTag:100]; 
if (!solidColor) { 

    solidColor = [[UIView alloc] initWithFrame:cell.bounds]; 
    solidColor.tag = 100; //Big tag to access the view afterwards 
    [cell addSubview:solidColor]; 
    [cell sendSubviewToBack:solidColor]; 
    [solidColor release]; 
} 
solidColor.backgroundColor = [UIColor colorWithRed:254.0/255.0 
              green:233.0/255.0 
               blue:233.0/255.0 
              alpha:1.0]; 

作品,即使有披露按鈕,並更好地爲你的邏輯對細胞色狀態行事的cellForRowAtIndexPath比我認爲cellWillShow事件。

3

創建一個視圖並將其設置爲小區

UIView *lab = [[UIView alloc] initWithFrame:cell.frame]; 
      [lab setBackgroundColor:[UIColor grayColor]]; 
      cell.backgroundView = lab; 
      [lab release]; 
2

要延長N.Berendt的答案的背景視圖 - 如果您想根據實際單元格的數據對象的一些狀態設置單元格顏色,在在配置表格單元格的其餘信息時(通常是在cellForRowAtIndexPath方法中)時,可以通過覆蓋willDisplayCell方法來設置內容視圖背景顏色的單元格背景顏色來實現此目的 - 獲取圍繞披露按鈕背景等問題不正確,但仍然可以讓您控制cellForRowAtIndexPath方法中的顏色,您可以在其中完成所有其他單元格定製。

所以: 如果添加此方法您的表視圖代表:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    cell.backgroundColor = cell.contentView.backgroundColor; 
} 

然後在你的cellForRowAtIndexPath方法,你可以這樣做:

if (myCellDataObject.hasSomeStateThatMeansItShouldShowAsBlue) { 
    cell.contentView.backgroundColor = [UIColor blueColor]; 
} 

這節省了檢索數據再次對象在willDisplayCell方法中,還可以保存兩個位置,您可以在其中定製/定製您的表格單元格 - 所有自定義都可以在cellForRowAtIndexPath方法中進行。

1

子類的UITableViewCell並在實施補充一點:

-(void)layoutSubviews 
{ 
    [super layoutSubviews]; 
    self.backgroundColor = [UIColor blueColor]; 
} 
3

創建作爲背景使用Photoshop或Gimp並將其命名爲MYIMAGE使用的圖像。 然後,此方法添加到您的tableViewController類:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    UIImage *cellImage = [UIImage imageNamed:@"myimage.png"];//myimage is a 20x50 px with gradient color created with gimp 
    UIImageView *cellView = [[UIImageView alloc] initWithImage:cellImage]; 
    cellView.contentMode = UIContentViewModeScaleToFill; 
    cell.backgroundView = cellView; 
    //set the background label to clear 
    cell.titleLabel.backgroundColor= [UIColor clearColor]; 
} 

這也將工作,如果你已經在屬性檢查器中設置的UITableView的自定義。

1
UIView *bg = [[UIView alloc] initWithFrame:cell.frame]; 
    bg.backgroundColor = [UIColor colorWithRed:175.0/255.0 green:220.0/255.0 blue:186.0/255.0 alpha:1]; 
    cell.backgroundView = bg; 
    [bg release]; 
6

嘗試所有不同的解決方案後,以下方法是最優雅的方法。 更改顏色以下委託方法:

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

試試這個:

- (void)tableView:(UITableView *)tableView1 willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [cell setBackgroundColor:[UIColor clearColor]]; 
    tableView1.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"Cream.jpg"]]; 
} 
1

這將在最新的Xcode工作。

-(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { 
    cell.backgroundColor = [UIColor grayColor]; 
} 
相關問題