2012-04-02 10 views
0

私はUITableViewCellsでアニメーションを実験したかったので、CALayersで簡単なものを試しました。私はすぐに問題に遭遇し、私は本当にここで何かを逃していると感じています。ここに私のコードです。CALayerとUITableViewCell conundrum

- (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