【问题标题】:Unexpected table cell animation after beginUpdates/endUpdatesbeginUpdates/endUpdates 后出现意外的表格单元格动画
【发布时间】:2014-01-28 10:55:40
【问题描述】:

我有一个带有静态单元格的表格视图。单击单元格会切换与该单元格关联的选取器视图单元格的可见性(更改单元格高度)。单击单元格的tag 属性维护选择器单元格的状态。动画由一系列beginUpdatesendUpdates 方法触发。这是一种为选取器单元格的可见性设置动画的简单方法(无需管理单元格插入和删除)。

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat defaultHeight = [super tableView:tableView heightForRowAtIndexPath:indexPath];

    switch (indexPath.row) {
        case RowIndexDayTimePicker:
            return self.dayTimeCell.tag > 0 ? defaultHeight : 0;

        case RowIndexPrivacyPicker:
            return self.privacyCell.tag > 0 ? defaultHeight : 0;

        default:
            return defaultHeight;
    }
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case RowIndexDayTime:
            self.dayTimeCell.tag *= -1;
            self.privacyCell.tag = -1;
            break;

        case RowIndexPrivacy:
            self.dayTimeCell.tag = -1;
            self.privacyCell.tag *= -1;
            break;

        default:
            break;
    }

    [tableView beginUpdates];
    [tableView endUpdates];
}

动画按预期工作,直到我将表格视图分隔符属性设置为 None 以使用自定义分隔符。比较两个屏幕截图(动画 gif):

我不知道如何解决它。我使用自动布局,选择器视图与超级视图对齐,每边的边距为零。在 iOS 7 模拟器和 iPos Touch 下测试。故事板截图:

【问题讨论】:

    标签: ios uitableview uiviewanimation


    【解决方案1】:

    看起来像一个 UITableView 错误。

    怎么样:

    self.tableView.separatorColor = [UIColor whiteColor]; //or clearColor?
    

    或者:

    self.tableView.separatorInset = UIEdgeInsetsMake(0, CGRectGetWidth(self.view.bounds), 0, 0);
    

    (目标是 hide 而不是 remove 分隔符。)

    【讨论】:

      【解决方案2】:

      我玩了很多表格单元格的高度变化动画,不幸的是我知道的所有方法都有一些问题。

      尝试使用UITableView这个方法:

      [UITableView reloadRowsAtIndexPaths:rowsToReload  withRowAnimation: UITableViewRowAnimationNone];
      

      同时尝试将UITableViewRowAnimationNone 更改为UITableViewRowAnimationAutomatic(我发现UITableViewRowAnimationNone 导致iPhone/iPod 上的动画不正确)

      对于我的项目,我以动画 [tableView reloadData] 方法结束:

      [UIView transitionWithView: self.tableView duration: 0.3 options: UIViewAnimationOptionTransitionCrossDissolve animations:^{
          [self.tableView reloadData];
      } completion: nil];
      

      【讨论】:

      • transitionWithView:duration:options:completion: 给出的动画略有不同。这是有效的解决方案。
      • @vokilam,你是对的,它提供了不同的动画,但对于我的问题,它是唯一一个可行的解决方案。
      【解决方案3】:

      我认为您的表正在重新加载两次。 删除以下代码,它会工作。

      [tableView beginUpdates]; 
      [tableView endUpdates];
      

      【讨论】:

      • 这条线使动画流畅。这是必需的行为。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-18
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      • 1970-01-01
      • 2014-12-02
      相关资源
      最近更新 更多