簡體   English   中英

設置分組的UITableViewCell的背景色

[英]Setting background color of grouped UITableViewCell

我有一個分組的UITableView 我試圖做一個自定義的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:@"rowbg@2x"]];

        //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;
}

背景顏色流過圓角。 見圖片: 在此處輸入圖片說明

我怎樣才能使它不流血?

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

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

那么self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

tableView:cellForRowAtIndexPath中設置單元格背景色

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

當沒有其他事情時,這對我有用。 將IB中的背景顏色設置為分組的表格視圖顏色(默認)。 然后在代碼中,將顏色設置為clearColor。 我也將單元格標記為opaque = NO和clearsContextBeforeDrawing = NO,但是這些設置本身並沒有改變任何東西,直到我通過代碼添加了clearColor – Bdebeez

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

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

我必須創建一個圓形的頂部,中間和按鈕圖形圖像,並根據它所在的行將其設置為單元格的背景視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM