2012-08-12 3 views
0

내가 - (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]; 

을 사용하여 테이블 셀에 UILabel의을 추가합니다.

테이블을 스크롤하거나 셀을 선택할 때까지 제대로 작동합니다. 그런 다음 레이블이 픽셀 화됩니다. 작업 후 enter image description here

:로드에서

enter image description here

는 또한 - (void) layoutSubviews에서 그것을 있는 UITableViewCell을 하위 클래스 레이블을 추가하고로드했습니다.

관련 질문이 이미 발견되었습니다. herehere하지만 아무런 효과가 없습니다.

편집 : 표준 셀 레이블은 이미 사용 중이므로 사용할 수 없습니다. 레이블을 추가해야합니다.

+0

어떤 iOS 버전을 사용하고 있습니까? 6.x라면 소프트웨어의 버그 일 수 있습니다 (5.x가 아니더라도 버그 일 수 있습니다 ... 아무도 완벽하지 않습니다). 배경을 투명하게 만들 수 있습니까? – FeifanZ

+0

iOS 5.1.1 및 iOS 6b4에서 테스트했습니다. 페이스 북과 트위터를 포함한 많은 앱들이 테이블 셀에 UILabels을 추가했기 때문에 버그라고 상상할 수 없습니다. –

+0

예, 배경을 투명하게 만들 수는 있지만 문제가 해결되지 않습니다. –

답변

0

나는 마침내 더러운 수정 작업을했습니다. 에서

- (있는 UITableViewCell *)있는 tableView : (jQuery과 *)있는 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]; 

후 나는이 두 가지 기능을 오버라이드 (override) :

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

희망이 어떤 사람들을 도와줍니다!

관련 문제