2009-05-22 141 views
33

我想要實現的效果是,表格視圖中的一個單元格將具有藍色背景,下一個單元格將具有白色,下一個單元格會再次呈現藍色,然後是白色,等等......你能讓我知道我該怎麼做?在iPhone上設置表格視圖單元格的背景顏色

謝謝。

+0

可能的重複:http://stackoverflow.com/questions/281515/how-to-customize-the-background-color-of-a-uitableviewcell – ilhnctn 2012-07-10 09:31:17

+1

@willcodejavaforfood這是一個非常無益的評論,是不相關的OP問題 – Max 2015-05-15 13:14:09

回答

39

必須設置單元格的內容視圖的背景顏色

cell.contentView.backgroundColor = [UIColor colorWithRed...] 

這將設置整個單元格的背景。

要爲備用電池做到這一點,在lostInTransit的答案作品使用indexPath.row%由2

+0

感謝您的回覆。我曾嘗試過,但它只顯示了單元格的兩端爲藍色/橙色,但我希望整個單元格具有顏色 – ebaccount 2009-05-25 17:05:37

+2

如果使用cell.backgroundColor,則會以藍色/橙色顯示兩端。如果使用cell.contentView.backgroundColor,則整個單元格將被着色,除非在單元格中具有任何具有白色背景的控件(如標籤)。在這種情況下,你將不得不改變他們的背景顏色。否則contentView.backgroundColor方法一直工作到現在。 – lostInTransit 2009-05-26 06:12:14

+0

我來這裏尋找如何改變整個單元格的背景顏色,但是發佈的問題並不完全相關,但是這個答案對我來說很有用,謝謝 – 2013-04-28 08:58:51

4

cell.contentView.backgroundColor = [UIColor colorWithRed...]方法,只要你不使用內置的一個UITableViewCell的標籤。

我發現如果您使用內置標籤,例如通過設置cell.text,您最終將在標籤下方顯示一個不透明的白色塊,並且只有單元格的兩端顯示所需的顏色。

我發現沒有辦法編輯內置標籤,因此它是不透明的(您可以通過UILabel* cellLabel = [cell.contentView.subviews objectAtIndex:0]訪問它)。

我通過添加自己的自定義UILabel來解決問題。就像這樣:

UILabel* cellLabel = [[[UILabel alloc] initWithFrame:cell.frame] autorelease]; 
cellLabel.text = @"Test with non-opaque label"; 
cellLabel.backgroundColor = [UIColor clearColor]; 
cellLabel.opaque = NO; 

[cell.contentView addSubview:cellLabel]; 
cell.contentView.backgroundColor = [UIColor darkGrayColor]; 
2

該代碼的情況下的稍微乾淨的解決方案,您使用的是內置的文本標籤和不希望的文本標籤模糊的背景色的白色背景顏色。這也修復違反表視圖的組合風格的圓角的問題(當你使用cell.contentView.backgroundColor代替cell.backgroundColor這恰好):

UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
cell.backgroundColor = [UIColor redColor]; 
cell.textLabel.backgroundColor = [UIColor clearColor]; //transparent background 
73

添加這個方法將你的表視圖委託:

#pragma mark UITableViewDelegate 
- (void)tableView: (UITableView*)tableView 
    willDisplayCell: (UITableViewCell*)cell 
forRowAtIndexPath: (NSIndexPath*)indexPath 
{ 
    cell.backgroundColor = indexPath.row % 2 
     ? [UIColor colorWithRed: 0.0 green: 0.0 blue: 1.0 alpha: 1.0] 
     : [UIColor whiteColor]; 
    cell.textLabel.backgroundColor = [UIColor clearColor]; 
    cell.detailTextLabel.backgroundColor = [UIColor clearColor]; 
} 
4

請添加以下代碼中的cellForRowAtIndexPath

if (indexPath.row % 2 == 0){ 
    cell.backgroundColor =[UIColor blueColor]; 
} else { 
    cell.backgroundColor =[UIColor whiteColor]; 
} 

我認爲這將有助於你

3

使用默認表格單元格設置時,以下兩個屬性將着色單元格的背景和其標籤的背景顏色,從而避免需要創建自定義標籤作爲單元格contentView的子視圖。

cell.textLabel.backgroundColor = [UIColor redColor]; 
cell.contentView.backgroundColor = [UIColor redColor]; 
9

如果您想根據實際單元格的數據對象的一些狀態設置單元格顏色,那麼這是另一種方法:

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

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

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

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

這節省了RET的在willDisplayCell方法中再次刪除數據對象。

3

//節省時間的方法:

//在索引方法細胞:

static NSString* evenIdentifier = @"even"; 
static NSString* oddIdentifier = @"odd"; 

__weak identifier; 
if(indexPath.row %2 ==0) 
{ 
    identifier = evenIdentifier; 
}else{ 
    identifier = oddIdentifier; 
} 

cell = dequeue..WithIdentifier: identifier; 
if(cell == nil){ 
    cell = allocOrLoadNib...; 
    cell.backgroundColor = (indexPath.row %2 ==0 ? evenColor : oddColor); 
} 

//更改單元格的內容,然後返回。輕鬆的工作。

//這是一個大綱代碼。請勿直接複製。

3

這爲我工作。在的cellForRowAtIndexPath,

cell.textLabel.backgroundColor = [UIColor clear]; 
cell.detailTextLabel.backgroundColor = [UIColor clearColor]; 
cell.contentView.backgroundColor = [UIColor grayColor]; //whichever color u want 
cell.backgroundColor = cell.contentView.backgroundColor; 

對於你的要求,如前面基於indexPath%2的值就可以執行上述功能提到。

0
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]; 
0

您只需在表格的視圖控制器實施文件中添加以下代碼即可。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
if (indexPath.row%2 == 0) { 
    UIColor *altCellColor = [UIColor blackColor]; 
    cell.backgroundColor = altCellColor; 
}} 

然後就像添加和更改模數值一樣簡單。

相關問題