设置分组UITableViewCell的背景颜色

时间:2011-08-30 06:41:03

标签: iphone objective-c cocoa-touch uitableview

我有一个分组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;
}

背景颜色从圆角溢出。见图片:enter image description here

我怎样才能使这不流血?

7 个答案:

答案 0 :(得分:4)

适用于iOS 3.0及更高版本:

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

答案 1 :(得分:1)

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;怎么样?

答案 2 :(得分:0)

当没有别的事情发生时,这对我有用。将IB中的背景颜色设置为分组的表视图颜色(默认)。然后在代码中,将颜色设置为clearColor。我还有标记为opaque = NO和clearsContextBeforeDrawing = NO的单元格,但是直到我通过代码添加clearColor之前,这些设置并没有改变任何东西 - Bdebeez

答案 3 :(得分:0)

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

答案 4 :(得分:0)

您是否尝试过设置backgroundView的backgroundColor而不是contentView?

答案 5 :(得分:0)

我必须创建一个圆形的顶部,中间和按钮图形图像,并根据它所在的行将其设置为单元格的背景视图。

答案 6 :(得分:0)

tableView:cellForRowAtIndexPath:

中设置单元格背景颜色
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage  imageNamed:@"Gradient.png"]];