0

I am trying to animate a UIImageview in a "table view cell", but the image never shows. Here is the code I'm trying to use.

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        cell.textLabel.font = [UIFont systemFontOfSize:16.0];
    }

    if (cell)
    {
        cell.textLabel.text = [titleArray objectAtIndex:indexPath.row];
        cell.textLabel.textColor = [UIColor darkGrayColor];

        UIImage *musicOne = [UIImage imageNamed:@"music1.png"];
        UIImage *musicTwo = [UIImage imageNamed:@"music2.png"];
        UIImage *musicThree = [UIImage imageNamed:@"music3.png"];

        NSArray *imagesArray = [NSArray arrayWithObjects:musicOne, musicTwo, musicThree, nil];

        if (indexPath.row == 0)
        {
            cell.imageView.animationImages = imagesArray;
            cell.imageView.animationDuration = 1.0f;
            cell.imageView.animationRepeatCount = 0;
            [cell.imageView startAnimating];
        }
    }

    return cell;
}

The image shows in the cell if I don't try to do animations with several images and just use one image in the image view.

cell.imageView.image = [imagesArray objectAtIndex:0];
raginggoat
  • 3,570
  • 10
  • 48
  • 108
  • And when are you running this code? And does the image view actually exist at the time? – Wain May 15 '14 at 14:13
  • @wain I've updated the question to show the entire method. – raginggoat May 15 '14 at 14:16
  • On its own the code looks reasonable. Try placing an image view on top of the table view and run the exact same code, see if it works. – Guy Kogus May 15 '14 at 14:17
  • So you never see any image, or you never see an animation? The code looks ok. Check the animation images just before the cell is displayed (and protect against reused cells still having the images animating...). – Wain May 15 '14 at 14:54
  • @wain I never see any image. I added the same code to viewDidLoad (and created another image view) and it worked there. – raginggoat May 15 '14 at 14:59

3 Answers3

3

It is necessary to set image property of cell.imageView too along with animationImages, otherwise UITableViewCell will show nothing.

In order to improve performance of the app and to save memory do following:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:@"music1.png"];
    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    UIImage *musicOne = [UIImage imageNamed:@"music1.png"];
    UIImage *musicTwo = [UIImage imageNamed:@"music2.png"];
    UIImage *musicThree = [UIImage imageNamed:@"music3.png"];
    NSArray *imagesArray = @[musicOne, musicTwo, musicThree];

    [cell.imageView setAnimationImages:imagesArray];
    cell.imageView.animationDuration = 1.0f;
    [cell.imageView startAnimating];
}

-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    [cell.imageView stopAnimating];
    [cell.layer removeAllAnimations];
} 
Sohail
  • 225
  • 1
  • 11
0

Try give the imageView a tag (e.g. 1) then get the image view like this:

    UIImage *musicOne = [UIImage imageNamed:@"music1.png"];
    UIImage *musicTwo = [UIImage imageNamed:@"music2.png"];
    UIImage *musicThree = [UIImage imageNamed:@"music3.png"];

    NSArray *imagesArray = [NSArray arrayWithObjects:musicOne, musicTwo, musicThree, nil];

    if (indexPath.row == 0)
    {
        UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];
        imageView.animationImages = imagesArray;
        imageView.animationDuration = 1.0f;
        imageView.animationRepeatCount = 0;
        [imageView startAnimating];
    }
Hao
  • 134
  • 4
0

This exact same code worked for me. I know in UIScrollView animations will stop when view is scrolling and for that reason you should use CADisplayLink to run you animation on NSRunLoopCommonModes instead of on NSDefaultRunLoopMode, but I have tested your code and it worked.

Pancho
  • 4,099
  • 1
  • 21
  • 32