iphone - 设置分组 UITableViewCell 的背景颜色

标签 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

怎样才能让它不流血?

最佳答案

这适用于 iOS 3.0 及更高版本:

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

关于iphone - 设置分组 UITableViewCell 的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7239545/

相关文章:

iphone - 如何在itunesconnect中访问旧版本应用程序的评论?

iphone - typedef 枚举 : which classes have access to it

objective-c - 使用 "struct sflt_filter"为 TCP 和 UDP 协议(protocol)设置数据过滤器

iphone - 如何在 iPhone sdk 中序列化一个简单的对象?

iphone - 位置管理器是在调度队列上创建的

iphone - 如何在 UIPageViewController 中的一页设置文本和另一页的图像

iphone - 如何在iOS中在PDF中绘制多种彩色文本?

iphone - "Statement requires expression of integer type"switch 语句和字符串数组错误

objective-c - 如何在 iPad 上分两列显示文本

objective-c - 如何从另一个 View 更改文本字段的文本