繁体   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