我的 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
中使用 AutoLayout
的 iOS 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/