2012-04-02 1 views
0

의 CALayer하고있는 UITableViewCell은 그래서 CALayers 몇 가지 간단한 물건을 시도했다. 나는 거의 즉시 문제에 부딪쳤다. 나는 정말로 내가 여기에 뭔가 빠져 있다고 느낀다. 여기 내 코드가있다.내가 UITableViewCells에서 애니메이션을 실험하고 싶었다 수수께끼

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MyIdentifier = @"MyIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]; 
    } 
    NSString* title = [self.array objectAtIndex:indexPath.row]; 
    cell.textLabel.text = title; 

    CALayer* layer = [CALayer layer]; 
    layer.backgroundColor = [UIColor greenColor].CGColor; 
    layer.bounds = CGRectMake(10, 10, 50, 50); 
    [cell.contentView.layer addSublayer:layer]; 

    return cell; 
} 

위의 코드는 예상대로 녹색 사각형을 추가합니다.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"did select"); 
    CALayer* layer = [CALayer layer]; 
    layer.backgroundColor = [UIColor purpleColor].CGColor; 
    layer.bounds = CGRectMake(50, 10, 50, 50); 
    UITableViewCell* cell = [self tableView:self.tableView cellForRowAtIndexPath:indexPath]; 
    [cell.contentView.layer addSublayer:layer]; 

    CGRect bounds = layer.bounds; 
    bounds.size.width = 200; 
    layer.bounds = bounds; 
} 

그러나이 코드는 예상대로 작동하지 않았습니다. 나는 자주색 사각형을 원했고 그 사각형은 더 큰 사각형으로 자랐습니다. 왜 그게 효과가 없었나요? 이 라인에서 실수가

답변

0

:

UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath]; 

첫 번째 정의 된 함수를 호출하고 당신이 그것을 부르는 새로운있는 UITableViewCell 객체를 생성 (또는 : 대신

UITableViewCell* cell = [self tableView:self.tableView cellForRowAtIndexPath:indexPath]; 

는, 이것을 사용 여기에서 중요하지 않은 다른 것을 대기열에서 제외). 올바른 행은 UITableView에서 지정된 행에있는 셀을 검색합니다. 이는 원하는 것입니다.

또한 1) 당신이 쓰는 당신이 이것을 달성하기 위해 몇 가지 CAAnimation 마법을 필요로하기 때문에,에 "다음 더 큰 사각형으로 성장할 것 보라색 사각형,"기대하지 않습니다. 2) 레이어의 조정 시스템이 다르기 때문에 .bounds 속성에주의하십시오. http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreAnimation_guide/Articles/Layers.html

관련 문제