2011-08-30 260 views
2

我有一個分組UITableView。我試圖使自定義背景UITableViewCell設置分組UITableViewCell的背景顏色

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 

     self.contentView.opaque = YES; 
     self.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"[email protected]"]]; 

     //Change Indicator 
     CGRect rect; 
     rect = CGRectMake(0, 0, 10, 50); 
     changeImageIndicator = [[UIImageView alloc] initWithFrame:rect]; 
     [self.contentView addSubview: changeImageIndicator]; 

     //Symbol 
     rect = CGRectMake(10, 10, 200, 20); 
     symbolLabel = [[UILabel alloc] initWithFrame:rect]; 
     symbolLabel.textAlignment = UITextAlignmentLeft; 
     symbolLabel.font = [UIFont fontWithName:@"Arial" size:22]; 
     symbolLabel.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0]; 
     symbolLabel.backgroundColor = [UIColor clearColor]; 
     [self.contentView addSubview: symbolLabel]; 

     //Company 
     rect = CGRectMake(10, 30, 180, 20); 
     companyLabel = [[UILabel alloc] initWithFrame:rect]; 
     companyLabel.textAlignment = UITextAlignmentLeft; 
     companyLabel.font = [UIFont fontWithName:@"Arial" size:13]; 
     companyLabel.adjustsFontSizeToFitWidth = YES; 
     companyLabel.minimumFontSize = 10.0; 
     companyLabel.backgroundColor = [UIColor clearColor]; 
     companyLabel.textColor = [UIColor colorWithRed:118.0/255.0 green:118.0/255.0 blue:118.0/255.0 alpha:1.0]; 
     [self.contentView addSubview: companyLabel]; 

     //Price 
     rect = CGRectMake(190, 10, 100, 20); 
     priceLabel = [[UILabel alloc] initWithFrame:rect]; 
     priceLabel.textAlignment = UITextAlignmentRight; 
     priceLabel.font = [UIFont fontWithName:@"Arial" size:20]; 
     priceLabel.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0]; 
     priceLabel.backgroundColor = [UIColor clearColor]; 
     [self.contentView addSubview: priceLabel]; 

     //Change 
     rect = CGRectMake(190, 30, 100, 20); 
     changeLabel = [[UILabel alloc] initWithFrame:rect]; 
     changeLabel.textAlignment = UITextAlignmentRight; 
     changeLabel.font = [UIFont fontWithName:@"Arial" size:15]; 
     changeLabel.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0]; 
     changeLabel.adjustsFontSizeToFitWidth = YES; 
     changeLabel.backgroundColor = [UIColor clearColor]; 
     changeLabel.minimumFontSize = 10.0; //adjust to preference obviously 

     [self.contentView addSubview: changeLabel]; 
    } 
    return self; 
} 

背景顏色出血過去的圓角。查看圖片:enter image description here

我該如何讓這個不流血?

回答

0

我必須創建一個倒圓的頂部,中部和按鈕的圖形圖像,並將其設置爲取決於哪個行它是單元格的背景圖。

0

這對我沒有任何其他的工作。將IB中的背景顏色設置爲分組的表格視圖顏色(默認)。然後在代碼中,將顏色設置爲clearColor。我也有單元格標記不透明= NO和clearsContextBeforeDrawing = NO,但這些設置本身並沒有改變任何東西,直到我通過代碼添加了clearColor - Bdebeez

+0

我嘗試做上述以下,這是確定 – Dashzaki

+0

我希望UITableView的背景顏色是條紋顏色。所以這是行不通的。我甚至用清晰的顏色嘗試過,效果相同。 –

+0

我不知道如何可以做到這一點,但可以更換條紋顏色條紋圖像 – Dashzaki

1

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

+0

仍然分隔符的風格沒有 –

0

self.contentView.superview.opaque = YES; self.contentView.superview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@「rowbg @ 2x」]];

+0

這將產生相同的結果 –

0

您是否嘗試過設置backgroundView的backgroundColor而不是contentView?

+0

這也有相同的結果沒有運氣 –

+0

哥們,做不下來的人投票試圖幫助。 – picciano

+0

我沒有downvote你 –

4

這適用於iOS 3.0及更高版本:

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

設置單元格背景顏色在的tableView:的cellForRowAtIndexPath:

cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Gradient.png"]];