2012-08-12 66 views
0

要將的UILabel添加到表格單元格我- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath使用像素化的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]; 

這工作正常,直到我滾動表或選擇一個單元格。然後標籤變成像素化。

在負載:enter image description here

行動後:enter image description here

我也試圖通過繼承的UITableViewCell添加標籤,並加載它在 - (void) layoutSubviews

我已經找到相關的問題herehere但是沒有任何工作。

編輯:它不可能使用標準單元格標籤,因爲它們已被使用。我需要添加一個額外的標籤。

+0

你在什麼版本的iOS?如果它是6.x,它可能是軟件中的一個錯誤(如果你不是5.x,它甚至可能是一個錯誤......沒有人是完美的)。是否有可能使背景不透明? – FeifanZ 2012-08-12 12:52:44

+0

我在iOS 5.1.1和iOS 6b4上測試了它。我無法想象這是一個錯誤,因爲包括Facebook和Twitter在內的許多應用程序都將UILabels添加到表格單元格中。 – 2012-08-12 13:00:12

+0

是的,有可能使背景不透明,但這並不能解決問題。 – 2012-08-12 13:18:10

回答

0

我終於得到它與一個骯髒的修復工作。

- (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]; 
    } 
} 

希望這可以幫助一些人!