【问题标题】:UITableViewCell animate improperly at the very first timeUITableViewCell 第一次动画不正确
【发布时间】:2014-04-15 22:20:36
【问题描述】:

bTableView 上的一个单元格被取消选择时,我实际上要删除aTableView 上的一个单元格。而且,我使用UIViewController 而不是UITableViewController 只是因为我的应用程序上有很多东西,而且可以肯定的是,我采用了UITableViewDelegateUITableViewDataSource

    - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
            if (tableView == bTableView) { ......
            MyCustomCell *cell = (MyCustomCell *)[aTableView cellForRowAtIndexPath:indexPath];
            [convertResultTableView beginUpdates];
            // some data updates stuff..

            [UIView beginAnimations:@"quitRotation" context:NULL];
            CATransform3D rotation;
            rotation = CATransform3DMakeRotation((70.0 * M_PI) / 180, 0.0, 0.0, 1.0);
            cell.layer.transform = rotation;
            cell.layer.anchorPoint = CGPointMake(-1, 0.5);
            cell.alpha = 0;
            cell.layer.shadowOffset = CGSizeMake(0, 0);
            [UIView commitAnimations];
            [convertResultTableView endUpdates];

问题是单元格只有在第二次、第三次、第四次动画时才能正确动画,但不是第一次。 (这意味着它出现、消失、出现和消失

它仍然会做一些动画,但并不像我预期的那样,而且那个错误的动画和正确的动画之间有很大的不同。

有什么想法吗?

【问题讨论】:

    标签: ios animation uitableview core-animation


    【解决方案1】:

    你应该尝试在方法 cellForRowAtIndexPath 中做这个动画

    类似这样的:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       self.selectedIndexPath = indexPath;
       [tableView reloadData];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      MyCustomCell *cell = (MyCustomCell *)[tableView cellForRowAtIndexPath:indexPath];
      if([self.selectedIndexPath isEqual: indexPath]) {
        // do the animation  
      }
    }
    

    【讨论】:

    • 感谢您的回复!很抱歉,当bTableView 上的一个单元格被取消选择时,我没有提到我实际上要删除aTableView 上的一个单元格。而且,我使用UIViewController 而不是UITableViewController 只是因为我的应用程序上有很多东西,当然,我采用了UITableViewDelegateUITableViewDataSource。无论如何,你能简单描述一下我为什么要在cellForRowAtIndexPath: 中做动画吗?谢谢!
    • 因为 cell 是某种东西,您不应该在方法 cellForRowAtIndexPath 之外触摸它。当您滚动时,单元格会自动从内存中释放,并且在显示之前,cellForRowAtIndexPath 方法正在设置单元格。如果您需要从第一个 tableview 中删除一些单元格,只需从数据源数组中删除项目并重新加载 table。
    • 知道了,我试试看:)
    【解决方案2】:

    真是一个愚蠢的错误!

    这是我的错误: 我的aTableView 在第一次显示时会做一些动画,但我的代码有一些愚蠢的错误,所以只有我的aTableView 的第一行单元格会做动画,因​​此 cell.layer.position 会有所不同。但是,在它消失并再次出现之后,cell.layer.position 都到了正确的位置,因为代码是正确的,所以它在第 2、第 3... 次时起作用。

    我是如何发现错误的: 我只是用NSLog(...); 来记录cell.layer 的所有属性,因为我对CoreAnimation 真的很陌生,幸运的是,我的第一次尝试是cell .layer.position,我马上就发现了错误。

    【讨论】:

      猜你喜欢
      • 2014-12-24
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-05
      • 1970-01-01
      相关资源
      最近更新 更多