找到你要的答案

Q:Why does my animation in my cell disappear when the cell is selected?

Q:为什么我的动画在我的细胞消失时,细胞被选中?

I have an animation as my image in one of my tableview's cells. When it is clicked, it turns the default blue and the image disappears! The navigation loads the new view, and when it I go back to the first view, hoping my animation returns, alas, there is no image :(. I don't do anything in the didSelectRowAtIndexPath function besides play a beep sound and push the new view. Any help is greatly appreciated.

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

  if (cell == nil) {
   cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
  }

  // Set up the cell…
  switch (indexPath.section) {
   case 0:
    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"TBR2.png"]];
    cell.primaryLabel.text = @"Text 2";
    cell.primaryLabel.backgroundColor = [UIColor clearColor]; 
    //textColor = [UIColor redColor];
    cell.secondaryLabel.text = @"Text 1";
    cell.secondaryLabel.backgroundColor = [UIColor clearColor]; 
    cell.myImageView.animationImages = [NSArray arrayWithObjects: 
             [UIImage imageNamed:@"star_events.png"],
             [UIImage imageNamed:@"star_events2.png"],
             [UIImage imageNamed:@"star_events3.png"],
             [UIImage imageNamed:@"star_events4.png"],
             [UIImage imageNamed:@"star_events5.png"],
             [UIImage imageNamed:@"star_events6.png"],
             [UIImage imageNamed:@"star_events7.png"],
             [UIImage imageNamed:@"star_events8.png"],
             [UIImage imageNamed:@"star_events9.png"],
             [UIImage imageNamed:@"star_events10.png"],
             [UIImage imageNamed:@"star_events11.png"],
             [UIImage imageNamed:@"star_events12.png"],
             [UIImage imageNamed:@"star_events13.png"],
             [UIImage imageNamed:@"star_events14.png"],
             [UIImage imageNamed:@"star_events16.png"],
             [UIImage imageNamed:@"star_events17.png"],
             [UIImage imageNamed:@"star_events18.png"],
             [UIImage imageNamed:@"star_events19.png"],
             [UIImage imageNamed:@"star_events20.png"],
             [UIImage imageNamed:@"star_events21.png"],
             [UIImage imageNamed:@"star_events22.png"],
             [UIImage imageNamed:@"star_events23.png"],
             [UIImage imageNamed:@"star_events24.png"],
             [UIImage imageNamed:@"star_events25.png"],
             [UIImage imageNamed:@"star_events26.png"],nil];

    // all frames will execute in .5 seconds
    cell.myImageView.animationDuration = .5;
    // repeat the annimation forever
    cell.myImageView.animationRepeatCount = 0;
    // start animating

    [cell.myImageView startAnimating];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    break;
   case 1:
    cell.primaryLabel.text = @"Label 1";
    cell.secondaryLabel.text = @"Search";
    cell.myImageView.image = [UIImage imageNamed:@"magglass.png"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    break;
   default:
    break;
  }
  return cell;
 }

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 NSString *path = [[NSBundle mainBundle] pathForResource:@"ping" ofType:@"wav"];

 AVAudioPlayer *myAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]error:NULL];
 myAudio.delegate = self;
 [myAudio play];

 [tableView deselectRowAtIndexPath:indexPath animated:YES];

  Page2 *page2 = [[Page2 alloc] initWithNibName:@"Page2" bundle:nil];
  [self.navigationController pushViewController:page2 animated:YES];
  [page2 release];
}

我有一个动画作为我的一个tableview细胞我的形象。当它被点击,它变成默认蓝色和图像消失!导航加载新的视图,当我回到第一个视图,希望我的动画返回,唉,没有图像。我什么都不做,在didselectrowatindexpath功能除了发挥嘟嘟的声音和推新观点。任何帮助不胜感激。

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

  if (cell == nil) {
   cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
  }

  // Set up the cell…
  switch (indexPath.section) {
   case 0:
    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"TBR2.png"]];
    cell.primaryLabel.text = @"Text 2";
    cell.primaryLabel.backgroundColor = [UIColor clearColor]; 
    //textColor = [UIColor redColor];
    cell.secondaryLabel.text = @"Text 1";
    cell.secondaryLabel.backgroundColor = [UIColor clearColor]; 
    cell.myImageView.animationImages = [NSArray arrayWithObjects: 
             [UIImage imageNamed:@"star_events.png"],
             [UIImage imageNamed:@"star_events2.png"],
             [UIImage imageNamed:@"star_events3.png"],
             [UIImage imageNamed:@"star_events4.png"],
             [UIImage imageNamed:@"star_events5.png"],
             [UIImage imageNamed:@"star_events6.png"],
             [UIImage imageNamed:@"star_events7.png"],
             [UIImage imageNamed:@"star_events8.png"],
             [UIImage imageNamed:@"star_events9.png"],
             [UIImage imageNamed:@"star_events10.png"],
             [UIImage imageNamed:@"star_events11.png"],
             [UIImage imageNamed:@"star_events12.png"],
             [UIImage imageNamed:@"star_events13.png"],
             [UIImage imageNamed:@"star_events14.png"],
             [UIImage imageNamed:@"star_events16.png"],
             [UIImage imageNamed:@"star_events17.png"],
             [UIImage imageNamed:@"star_events18.png"],
             [UIImage imageNamed:@"star_events19.png"],
             [UIImage imageNamed:@"star_events20.png"],
             [UIImage imageNamed:@"star_events21.png"],
             [UIImage imageNamed:@"star_events22.png"],
             [UIImage imageNamed:@"star_events23.png"],
             [UIImage imageNamed:@"star_events24.png"],
             [UIImage imageNamed:@"star_events25.png"],
             [UIImage imageNamed:@"star_events26.png"],nil];

    // all frames will execute in .5 seconds
    cell.myImageView.animationDuration = .5;
    // repeat the annimation forever
    cell.myImageView.animationRepeatCount = 0;
    // start animating

    [cell.myImageView startAnimating];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    break;
   case 1:
    cell.primaryLabel.text = @"Label 1";
    cell.secondaryLabel.text = @"Search";
    cell.myImageView.image = [UIImage imageNamed:@"magglass.png"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    break;
   default:
    break;
  }
  return cell;
 }

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 NSString *path = [[NSBundle mainBundle] pathForResource:@"ping" ofType:@"wav"];

 AVAudioPlayer *myAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]error:NULL];
 myAudio.delegate = self;
 [myAudio play];

 [tableView deselectRowAtIndexPath:indexPath animated:YES];

  Page2 *page2 = [[Page2 alloc] initWithNibName:@"Page2" bundle:nil];
  [self.navigationController pushViewController:page2 animated:YES];
  [page2 release];
}
answer1: 回答1:

I had the same problem on iOS 8.4 and onwards and I've found a solution. Maybe it's not very good but it works.

I figured out that the problem is only with the animated image and only when the image changes its state to highlighted. So my workaround for UIImageView is as follows:

  • Subclass UIImageView
  • Override setHighlighted method to exclude highlighted state from animated image.

My setHighlighted method looks like:

- (void)setHighlighted:(BOOL)highlighted {

    if (!self.isAnimating) {
        [super setHighlighted:highlighted];
    }
}

Also, If you don't need visible selection on the cell you can set selectionStyle property to UITableViewCellSelectionStyleNone of your table.

我已经在iOS 8.4起同样的问题,我已经找到了一个解决方案。也许它不是很好,但它的作品。

我发现这个问题只与动画图像,只有当图像改变其状态突出。所以我的解决方案为UIImageView如下:

  • Subclass UIImageView
  • Override setHighlighted method to exclude highlighted state from animated image.

我sethighlighted方法看起来像:

- (void)setHighlighted:(BOOL)highlighted {

    if (!self.isAnimating) {
        [super setHighlighted:highlighted];
    }
}

另外,如果你不需要可见选择对细胞可以设置uitableviewcellselectionstylenone你selectionstyle属性表。

iphone  objective-c  cocoa-touch  uitableview  uikit