2013-03-04 4 views
1

iOS에서는 UITableViewCell 레이어에 CALayer를 추가하고 있습니다. 이것은 CALayer를 처음 사용하는 것으로 단순히 표 셀의 배경색을 변경하기로되어 있습니다. 내 목표는 (1) CALayer 사용법을 배우고, (2) CGContextFillRect에서 속도가 느려지는 드로잉이 현재 구현보다 빠르다는 것을 Instrument를 사용하여 테스트하는 것입니다.CALayer를 사용하여 UITableViewCell 배경색 설정

(Technical Q&A QA1708이 모든 촉매이었다.)

현재 구현 (작품)

- (void)drawRect:(CGRect)r 
{ 
    UIColor *myColor = [self someColor]; 
    [myColor set]; 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextFillRect(context, r); // draw the background color 
    // now draw everything else 
    // [...] 

} 

시도 새로운 구현

#import <QuartzCore/QuartzCore.h> 

@implementation MyCell { 
    CALayer *backgroundLayer; 
} 

- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 

    if (self) { 
     // [...other stuff here too] 
     backgroundLayer = [[CALayer alloc] init]; 
     [[self layer] addSublayer:backgroundLayer]; 
    } 

    return self; 
} 

- (void)drawRect:(CGRect)r { 
    backgroundLayer.frame = CGRectMake(0, 0, r.size.width, r.size.height); 
    [backgroundLayer setBackgroundColor:[self someColor]]; 
    // now draw everything else 
    // [...] 
} 

을 (작동하지 않습니다) 나는 올바른 참조 색상,하지만 다른 도면 (나는 사용자 정의 드로잉 뒤에 내 새로운 레이어를 끝내고 있다고 가정합니다).

backgroundLayer.frame = ... 줄을 제거하면 내 모든 다른 그림이 검정색 배경에 그대로 남아 있습니다.

무엇이 누락 되었습니까?

+2

'insertSublayer으로 시도 : atIndex : 0' 대신 addSublayer'의'. 그러면 레이어가 하위 레이어 배열의 맨 아래에 놓입니다. OS는'zPosition' 레이어와 sublayers 배열의 상대 위치를 사용하여 어떤 레이어가 보이고 어떤 것이 차단되는지를 결정합니다. –

+0

@Aaron Brager 왜 여기에 'CALayer'를 사용하고 있습니까? 'UIView'에서 이미 할 수없는 일로 무엇을하고 싶습니까? –

+0

@ DanielMartín 그건 작동하지 않았다. 인덱스 0과 1에서 검정색 배경을 얻습니다. 2 이상 내 그림이 보이지 않습니다. –

답변

3

예기치 않은 동작을 얻고있는 이유 때문에 UITableViewCell 년대 상대적으로 복잡 뷰 계층 구조입니다 : 당신이 UITableViewCell에서 사용자 지정 그리기 루틴을 정의 할 때마다, 당신은 contentView 계층 구조 내에서 그렇게해야

- UITableViewCell 
    - contentView 
    - backgroundView 
    - selectedBackgroundView 

. 여기에는 UIView의 서브 클래 싱, -drawRect:을 오버라이드하고 서브 뷰로 contentView에 추가하는 작업이 포함됩니다.

귀하의 배경색이 무시되는 이유는 귀하의 CALayerUITableViewCell 님의 레이어의 하위 레이어로 추가했기 때문입니다. 이것은 UITableViewCell 님의 contentView에 의해 가려집니다.

그러나 어떤 이유로 여기에 CALayer을 사용하려고합니다. UIView에는없는 항목이없는 이유를 이해하고 싶습니다. 이 로터리 설정 대신 contentViewbackgroundColor을 설정할 수 있습니다.

다음은 요청에 따라 CALayer를 사용하는 예제입니다 :

@implementation JRTableViewCell 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if(self) { 
     [self addCustomLayerToContentView]; 
    } 
    return self; 
} 

- (void)addCustomLayerToContentView { 
    CALayer *layer = [[CALayer alloc] initWithFrame:[self bounds]]; 
    [layer setBackgroundColor:[UIColor blueColor]]; //use whatever color you wish. 

    [self.contentView.layer addSublayer:layer]; 
} 

@end 
+2

감사합니다. 이 변경으로'drawRect' 함수의 실행 시간이 2 초 단축되었습니다. 이제 테이블이 40-45FPS 대신 초당 56-58 프레임 씩 스크롤됩니다. –

+2

'initWithFrame :'메소드가없고'setBackgroundColor :'는'UIColor'가 아닌'CGColorRef'를 취합니다. – pr1001

관련 문제