Пиксельный UILabel в ячейке таблицы

Чтобы добавить 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]; 

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

Это отлично работает, пока я не прокручу таблицу или не выберу ячейку. Затем этикетка становится пиксельной.

При загрузке: введите описание изображения здесь

после действия: введите описание изображения здесь

Я также попытался добавить метку, создав подкласс UITableViewCell и загрузив его в - (void) layoutSubviews.

Я уже нашел связанные вопросы ​​здесь и здесь, но ничего работал.

РЕДАКТИРОВАТЬ: невозможно использовать стандартные метки ячеек, поскольку они уже используются. Мне нужно добавить дополнительный ярлык.


person Alexander Meiler    schedule 12.08.2012    source источник
comment
На какой у вас версии iOS? Если это 6.x, это может быть ошибка в программном обеспечении (это может быть даже ошибка, если вы не используете 5.x… никто не идеален). Можно ли сделать фон непрозрачным?   -  person FeifanZ    schedule 12.08.2012
comment
Тестировал на iOS 5.1.1 и iOS 6b4. Я не могу себе представить, что это ошибка, поскольку многие приложения, включая Facebook и Twitter, добавляют UILabels в ячейку таблицы.   -  person Alexander Meiler    schedule 12.08.2012
comment
да, можно сделать фон непрозрачным, но это не решает проблему.   -  person Alexander Meiler    schedule 12.08.2012


Ответы (1)


Наконец-то я получил грязное исправление.

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

Я установил

cell.selectionStyle = UITableViewCellSelectionStyleNone;.

В подклассе UITableViewCell я загружаю timeLabel в initWithStyle следующим образом:

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:@"[email protected]"];
        //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:@"[email protected]"];
        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];
    }
}

надеюсь, что это поможет некоторым людям!

person Community    schedule 12.08.2012