2013-01-22 27 views
0

我使用這個代碼來定義我的手機背景圖的顏色:不能設置自定義BACKGROUNDCOLOR上的UITableViewCell

UIColor *cellBackgroundColor = [UIColor colorWithRed:255.0 green:255.0 blue:255.0 alpha:1.0]; 
cell.contentView.backgroundColor = cellBackgroundColor; 

的事情是,這是行不通的。此代碼雖然工作得很好:

cell.contentView.backgroundColor = [UIColor redColor]; 

這裏有什麼問題?我不能根據自己的喜好自定義單元格的顏色嗎?或者我做錯了什麼?

+0

沒有看到的顏色,在顏色是透明的? –

回答

2

你必須設置單元格背景與此委託: -

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

    // UIImage *img = [UIImage imageNamed:@"button.png"]; // get your background image 

    UIColor *cellBackgroundColor = [UIColor colorWithRed:255/255.0 green:108/255.0 blue:61/255.0 alpha:1.0]; //if you want to set color then use this line 

    // UIColor *backgroundColor = [[UIColor alloc] initWithPatternImage: img]; 
    cell.backgroundColor = cellBackgroundColor; 
    cell.textLabel.backgroundColor = [UIColor clearColor]; 
    cell.detailTextLabel.backgroundColor = [UIColor clearColor]; 
} 

您的表格如下所示: -

enter image description here

Taking an RGB color and normalizing it with UIColor on the iPhone

Your values are between 0 and 255. Use them to create a UIColor: 

    float r; float g; float b; float a; 

    [UIColor colorWithRed:r/255.f 
        green:g/255.f 
        blue:b/255.f  
        alpha:a/255.f]; 
+0

反正這些/255.0是什麼?我不能直接將Photoshop中的顏色數據複製並粘貼到UIColor中? –

+0

在計算機中,組件值通常以整數形式存儲在0到255的範圍內,即單個8位字節可提供的範圍。這些通常表示爲十進制或十六進制數字。閱讀更多在http://en.wikipedia.org/wiki/RGB_color_model –

+0

所以,據我所知,要在Photoshop中獲得230的R值,我必須使用UIColor colorWithRed:230/255.f?或者它有什麼不同? –

2

只是改變:

UIColor *cellBackgroundColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0]; 
cell.contentView.backgroundColor = cellBackgroundColor; 
+1

如果它不起作用,除以256.0 – vshall

1
cell.backgroundView.backgroundColor 

你可以做一個自定義視圖,可以改變整個backgroundview

UIView *backgroundViewForCell = [[[UIView alloc] init] autorelease]; 
backgroundViewForCell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"row-bg.png"]]; 
UIView *selectedBackgroundViewForCell = [[[UIView alloc] init] autorelease]; 
selectedBackgroundViewForCell.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:.2]; 
cell.backgroundView = backgroundViewForCell; 
cell.selectedBackgroundView = selectedBackgroundViewForCell; 
0

你是在正確的軌道上。您只需用此代替您的顏色代碼

UIColor *cellBackgroundColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0]; 

享受編程!

1

也許你可以在設置顏色之前添加cell.contentView.backgroundColor = [UIColor clearColor]

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

其他邏輯

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