0

要将UILabel添加到我使用的表格单元格

UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(270, 10, 40, 12)];
timeLabel.text = @"2s";
timeLabel.backgroundColor = [UIColor clearColor];
timeLabel.font = [UIFont systemFontOfSize:12];
timeLabel.textColor = [UIColor lightGrayColor];
timeLabel.highlightedTextColor = [UIColor whiteColor];
timeLabel.textAlignment = UITextAlignmentRight;
timeLabel.frame = CGRectIntegral(timeLabel.frame);
[cell.contentView addSubview:timeLabel]; 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath.

这工作正常,直到我滚动表格或选择一个单元格。然后标签变得像素化。

负载时:在此处输入图像描述

行动后:在此处输入图像描述

我还尝试通过子类化UITableViewCell来添加标签并将其加载到 - (void) layoutSubviews.

我已经在这里这里找到了相关的问题,但没有任何效果。

编辑:不可能使用标准单元格标签,因为它们已经在使用中。我需要添加一个额外的标签。

4

1 回答 1

0

我终于得到了一个肮脏的修复。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

我设置

cell.selectionStyle = UITableViewCellSelectionStyleNone;.

UITableViewCell的子类中,我在initWithStyle中加载 timeLabel,如下所示:

timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(270, 10, 40, 12)];
timeLabel.text = @"2s";
timeLabel.backgroundColor = [UIColor whiteColor];
timeLabel.font = [UIFont systemFontOfSize:12];
timeLabel.textColor = [UIColor lightGrayColor];
timeLabel.highlightedTextColor = [UIColor whiteColor];
timeLabel.textAlignment = UITextAlignmentRight;
[self.contentView addSubview:timeLabel];

然后我覆盖这两个函数:

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    if(highlighted == YES){
        UIImage *image = [UIImage imageNamed:@"cellBg@2x.png"];
        //scale custom cell background to necessary height
        UIImage *scaledImage = [image scaleToSize:CGSizeMake(1,self.contentView.frame.size.height)];
        //set cell background
        self.backgroundColor = [UIColor colorWithPatternImage:scaledImage];
        //set textcolor for default labels
        self.textLabel.textColor = [UIColor whiteColor];
        self.detailTextLabel.textColor = [UIColor whiteColor];
        //set textcolor for custom label
        timeLabel.textColor = [UIColor whiteColor]; 
        //cope background for custom label background since timeLabel.backgroundColor = [UIColor clearColor] doesnt work
        CGImageRef ref = CGImageCreateWithImageInRect(scaledImage.CGImage, CGRectMake(0, 10, 12, 20));
        UIImage *img = [UIImage imageWithCGImage:ref];
        //set custom label background
        timeLabel.backgroundColor = [UIColor colorWithPatternImage:img];
    } else {
        //set unselected colors
        self.backgroundColor = [UIColor whiteColor];
        self.textLabel.textColor = [UIColor darkGrayColor];
        self.detailTextLabel.textColor = UIColorFromRGB(0x808080);
        timeLabel.textColor = UIColorFromRGB(0x808080);
        //white background works without the label pixelates
        timeLabel.backgroundColor = [UIColor whiteColor];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    if(selected == YES){
        UIImage *image = [UIImage imageNamed:@"cellBg@2x.png"];
        UIImage *scaledImage = [image scaleToSize:CGSizeMake(1,self.contentView.frame.size.height)];
        self.backgroundColor = [UIColor colorWithPatternImage:scaledImage];
        self.textLabel.textColor = [UIColor whiteColor];
        self.detailTextLabel.textColor = [UIColor whiteColor];
        timeLabel.textColor = [UIColor whiteColor];
        CGImageRef ref = CGImageCreateWithImageInRect(scaledImage.CGImage, CGRectMake(0, 10, 12, 20));
        UIImage *img = [UIImage imageWithCGImage:ref];  
        timeLabel.backgroundColor = [UIColor colorWithPatternImage:img];
    } else {
        self.backgroundColor = [UIColor whiteColor];
        self.textLabel.textColor = [UIColor darkGrayColor];
        self.detailTextLabel.textColor = UIColorFromRGB(0x808080);
        timeLabel.textColor = UIColorFromRGB(0x808080);
        timeLabel.backgroundColor = [UIColor whiteColor];
    }
}

希望这可以帮助一些人!

于 2012-08-12T15:34:53.287 回答