【问题标题】:Why does my animation in my cell disappear when the cell is selected?为什么选择单元格时我的单元格中的动画会消失?
【发布时间】:2010-10-22 21:18:18
【问题描述】:

我在我的 tableview 的一个单元格中有一个动画作为我的图像。当它被点击时,它变成默认的蓝色并且图像消失了!导航加载新视图,当我返回第一个视图时,希望我的动画返回,唉,没有图像:(。除了播放哔声并按下新视图。非常感谢任何帮助。

- (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];
}

【问题讨论】:

  • 考虑使用
     作为代码,它非常不可读。
  • Aleph:我在修复标签的同时修复了它。
  • 你试过“[tableView reloadData];”什么时候回到第一个视图?
  • 加动画的时候能不能设置animation.removedOnCompletion = NO;

标签: iphone objective-c cocoa-touch uitableview uikit


【解决方案1】:

我在 iOS 8.4 及更高版本上遇到了同样的问题,我找到了解决方案。也许它不是很好,但它确实有效。

我发现问题仅出在动画图像上,并且仅在图像将其状态更改为突出显示时。所以我对UIImageView 的解决方法如下:

  • 子类UIImageView
  • 覆盖 setHighlighted 方法以从动画图像中排除突出显示的状态。

我的setHighlighted 方法如下:

- (void)setHighlighted:(BOOL)highlighted {

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

另外,如果您不需要单元格上的可见选择,您可以将selectionStyle 属性设置为表格的UITableViewCellSelectionStyleNone

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-15
    • 2012-03-17
    • 1970-01-01
    相关资源
    最近更新 更多