objective-c - iOS 10 中的 UITableViewCell 动画高度问题

标签 objective-c ios10

我的 UITableViewCell 将在识别点击时为其高度设置动画。在 iOS 9 及以下版本中,这个动画很流畅,没有问题。在 iOS 10 beta 中,动画期间有一个不和谐的跳跃。有办法解决这个问题吗?

这是代码的基本示例。

- (void)cellTapped {
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.shouldExpandCell ? 200.0f : 100.0f;
}

编辑:2016 年 9 月 8 日

问题在 GM 中仍然存在。经过更多调试后,我发现问题与单元格立即跳到新高度有关,然后将动画相关单元格偏移量。这意味着任何依赖于单元格底部的基于 CGRect 的动画都将不起作用。

例如,如果我将 View 限制在单元格底部,它将跳转。该解决方案将涉及使用动态常数对顶部的约束。或者想出另一种方式来定位与底部相关的 View 。

最佳答案

Swift 中使用 AutoLayoutiOS 10 解决方案:

将此代码放入您的自定义 UITableViewCell

override func layoutSubviews() {
    super.layoutSubviews()

    if #available(iOS 10, *) {
        UIView.animateWithDuration(0.3) { self.contentView.layoutIfNeeded() }
    }
}

在 Objective-C 中:

- (void)layoutSubviews
{
    [super layoutSubviews];

    NSOperatingSystemVersion ios10 = (NSOperatingSystemVersion){10, 0, 0};
    if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:ios10]) {
        [UIView animateWithDuration:0.3
                         animations:^{
                             [self.contentView layoutIfNeeded];
                         }];
    }
}

如果您像下面这样配置了 UITableViewDelegate,这将使 UITableViewCell 改变高度:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return selectedIndexes.contains(indexPath.row) ? Constants.expandedTableViewCellHeight : Constants.collapsedTableViewCellHeight
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
    tableView.beginUpdates()
    if let index = selectedIndexes.indexOf(indexPath.row) {
        selectedIndexes.removeAtIndex(index)
    } else {
        selectedIndexes.append(indexPath.row)
    }
    tableView.endUpdates()
}

关于objective-c - iOS 10 中的 UITableViewCell 动画高度问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39104846/

相关文章:

ios - iMessage 应用程序缺少所需的 iMessage 应用程序扩展 - 无法在设备上运行

ios - AFNetworking 是在没有完整位码的情况下构建的

iphone - 如何在 iphone 应用程序的搜索表中显示第一个词

objective-c - 带搜索字段的 Cocoa 绑定(bind)可变字典

IOS 10 语音识别 API : Error Domain=kAFAssistantErrorDomain Code=1101

ios - 如何禁用 native 调用屏幕中的消息选项?

ios - 如何为uitableview自定义多选编辑模式

objective-c - 哪种方法更好 : retrieve images from AWS S3 or download it and store locally in a temp folder to be displayed?

ios - Apple Pay 沙盒测试器无法登录设备 iOS 10

Facebook 登录错误 - Xcode 8 GM