2013-03-29 86 views
2

我有一個問題,其中設置我的tableView的背景顏色覆蓋我爲單個單元格設置的背景顏色。更具體地講,我有3行的表:頂部細胞是暗灰色,底部2個細胞是紅:iOS UITableView:改變表格的背景顏色覆蓋單元格的背景顏色

http://imgur.com/otYlFMx,LvoLDzY,tXiJenw#0

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UIColor *sectionBackgroundColor = [UIColor colorWithRed:0.09f green:0.09f blue:0.09f alpha:1.0]; 
    UIColor *menuItemBackgroundColor = [UIColor redColor]; 

    if (indexPath.row == 0) { 
     cell.backgroundColor = sectionBackgroundColor; 
     cell.textLabel.textColor = [UIColor whiteColor]; 
     cell.textLabel.text = @"MENU"; 
     cell.textLabel.font= [UIFont systemFontOfSize:16.0]; 
     cell.selectionStyle = false; 
    } else if (indexPath.row == 1) { 
     cell.backgroundColor = menuItemBackgroundColor; 
     cell.textLabel.textColor = [UIColor whiteColor]; 
     cell.textLabel.text = @"Schedule"; 
     cell.textLabel.font= [UIFont systemFontOfSize:18.0]; 
     cell.imageView.image = [UIImage imageNamed:@"Schedule.png"]; 
    } else if (indexPath.row == 2) { 
     cell.backgroundColor = menuItemBackgroundColor; 
     cell.textLabel.textColor = [UIColor whiteColor]; 
     cell.textLabel.text = @"Contacts"; 
     cell.textLabel.font= [UIFont systemFontOfSize:18.0]; 
     cell.imageView.image = [UIImage imageNamed:@"Contacts.png"]; 
    } else { 
     cell.backgroundColor = [UIColor blackColor]; 
     cell.selectionStyle = false; 
    } 

} 

我想要做的就是讓所有底部黑色的那些空的,未使用的白色細胞。根據我的研究,設置的tableView背景顏色將做到這一點:

http://imgur.com/otYlFMx,LvoLDzY,tXiJenw#1

tableView.backgroundColor = [UIColor blackColor]; 

然而,當你注意到沒有,這行代碼改變了前兩名單元格的背景顏色爲黑色了。我打得四處這個越來越注意到,每一個單元格的背景顏色將變爲黑色,除了最後一個通用的模式:對於所發生的事情

(見上面的鏈接3圖像)

任何想法這裏?幫助將不勝感激!



有些事情我已經試過,不工作(我會提供更多的聯繫,但我現在代表不允許我):

a)提出的意見細胞不透明。更改單元格中不同視圖的顏色。

cell.opaque = YES; 
cell.backgroundView.opaque = YES; 
cell.contentView.opaque = YES; 
cell.backgroundColor = menuItemBackgroundColor; 
cell.contentView.backgroundColor = menuItemBackgroundColor; 
cell.backgroundView.backgroundColor = menuItemBackgroundColor; 

B)設置tableView的backgroundView無。

tableView.backgroundView = nil; 

C)設置tableView的backgroundView背景顏色。

tableView.backgroundView.backgroundColor = [UIColor blackColor]; 
+0

backgroundView默認爲零,所以cell.backgroundView.backgroundColor = menuItemBackgroundColor;除非您添加了背景視圖,否則不會執行任何操作。 – rdelmar

回答

4

設置我的tableView的背景顏色將覆蓋我爲我的單個細胞

這是正確的設置背景顏色。討厭,不是嗎? :)問題其實就是給單元格上色backgroundView(比聽起來容易得多),或者等到tableView:willDisplayCell:forRowAtIndexPath:,並將單元格的backgroundColor設置在那裏。

+0

是的,這很煩人!不幸的是,如我的帖子所示,我已經嘗試了這兩個,他們不工作:( – Chun

+0

@Chun,你看到我的評論嗎?你添加了一個backgroundView? – rdelmar

+0

啊,我現在看到。 – Chun

0

以下的解決方法允許您使用在故事板指定的背景顏色,避免在您的代碼中設置明確的顏色:

  • dequeueReusableCellWithIdentifier:,而從故事板的背景顏色仍然完好,保存,例如在您的自定義UITableView子類的屬性中。
  • 稍後,在willDisplayCell:中,使用您在dequeueReusableCellWithIdentifier:中保存的值恢復backgroundColor