2011-11-11 5 views
1

하위 클래스 UITableViewCell 안에 서브 클래스 UIButton이 있습니다. 단추는 펜촉에서로드됩니다.CALayer 렌더링 또는 그리기가 없음

버튼이있는 이미지로 사용하고 싶습니다. 애니메이션에 대한 더 많은 제어를 위해 CALayer을 사용하고 싶습니다. 사용자가 버튼을 탭하면 애니메이션이 발생합니다. 그러나, 나는 심지어 이미지가 나타나기까지 할 수 없다.

QuartzCore를 가져 왔습니다. 내 UIButton를 서브 클래스 (항상 펜촉에서로드)의

코드 :

-(void)awakeFromNib { 
    [super awakeFromNib]; 
    self.clipsToBounds = NO; 
    self.backgroundColor = [UIColor clearColor]; 
    self.opaque = NO; 
    self.userInteractionEnabled = YES; 
} 

코드 테이블 뷰 컨트롤러는 :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    MyCustomCell *cell = (BLCustomCellCenter*)[tableView dequeueReusableCellWithIdentifier:@"customCellID"]; 
    if (cell == nil) { 
     // ... cell is initialized 
    } 

    // configure the image for the button 
    // 
    // Note: there is an outlet to the UIButton called 'customButton' 

    CALayer *imgLayer = [CALayer layer]; 
    imgLayer.bounds = CGRectMake(0, 0, cell.customButton.bounds.size.width, cell.customButton.bounds.size.height); 
    imgLayer.position = CGPointMake(cell.customButton.bounds.size.width/2, cell.customButton.bounds.size.height/2); 
    UIImage *img = [UIImage imageNamed:@"someImage"]; 
    imgLayer.contents = (id)[img CGImage]; 
    [cell.customButton.layer addSublayer:imgLayer]; 

    // ... configure subviews of 'customButton' 

    return cell; 
} 

어떤 도움은 매우 언제나처럼, 감사합니다.

답변

1

몇 시간 동안 디버깅 한 후에 나타났습니다. 물건 (아마 내가 언급하는 것을 잊었다)은 주문 UITableViewCell가 펜촉에서로드되었다라는 것이었다. 보려는 하위보기도 펜촉에서로드되었습니다. 그래서 펜촉의 펜촉입니다.

this wonderful article, 서브 뷰의 클래스 내에있는 awakeAfterUsingCoder:을 오버라이드하는 것이 트릭을 만들었습니다. 부모 펜촉을로드 할 때 super의 initUsingCoder:이 자리 표시 자 객체 만로드하는 하위보기에서 호출됩니다.

내가 원하는 실제 개체 대신 개체 틀을 조작 할 때이 '개체 틀 개체'가 내 문제를 일으켰습니다.

따라서 하위 뷰의 객체를로드하고 초기화해야합니다.이 작업은 NSObjectawakeAfterUsingCoder:을 재정 의하여 수행해야합니다.

0

내용을 설정 한 후에 [imgLayer setNeedsDisplay];을 추가하십시오.

+0

아니, 그런 행운. –

+0

img가 0이 아닌 것을 확인 했습니까? –

+1

그것은 0이 아닙니다. NSLog는 ""과 같은 피드백을 제공합니다. –