views:

316

answers:

1

Hey guys, (relatively, I believe) simple question here,

I have a UITableViewController with its UITableView, with its determined number of cells. If a user taps on a cell, an image is inserted into the respective cell's imageView property, like so:

[self.tableView cellForRowAtIndexPath:chosenPersonIndexPath].imageView.image = [UIImage imageNamed:@"tick.jpeg"];

(where chosenPersonIndexPath is simply the selected cell's index path)

Is there any way to animate this? As is, the UITableViewController simply sticks in the image with no transition whatsoever. All I'm asking is if there is a way to possibly animate this, and if so, how?

Many thanks in advance! ~Joaquim

A: 

Given that UIImageView instances are UIView instances, you can animate them just like any other UIView:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.imageView.alpha = 0.0;
    cell.imageView.image = [UIImage imageNamed:@"tick.jpg"];
    cell.textLabel.text = @"tap!";

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.4];
    [self.tableView cellForRowAtIndexPath:indexPath].imageView.alpha = 1.0;
    [UIView commitAnimations];    
}

The animation in the tableView:didSelectRowAtIndexPath: method could include flips, size changes, or any other kind of (animatable) property change. Check out for the "Animatable Properties" chapter in the "Core Animation Programming Guide" in the iPhone SDK documentation for more information.

Hope this helps!

Adrian Kosmaczewski
Awesome! That's exactly what I was looking for. Thanks so much for your patience in answering a 16-year-old rookie dev's 'newbish' question. :D Happy 2010!
wakachamo
No problem! :) glad to help.
Adrian Kosmaczewski