我在使单元折叠动画看起来始终平滑时遇到问题。
我的形象抵得上一千个字。这是同一表视图的两个 GIF:
折叠对于单元格“6”非常有效,但对于“5”则不然。看起来整个 UITableView 内容在执行折叠动画之前都会跳起来。
我没有使用UITableViewAutomaticDimensionUITableView's rowHeight。相反,我通过方法提供行高,并使用表视图的 / 扩展/tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath)折叠单元格。正如您所看到的,我还使用滚动到展开的行(但仅在展开时,所以这可能并不重要)。beginUpdatesendUpdatestableView.scrollToRow(at: indexPath, at: .none, animated: true)
UITableViewController 的子类嵌入在具有大标题和搜索栏的 UINavigationController 中(此处不可见,因为内容向下滚动)。UINavigationController 嵌入在 UITabBarController 中。
这是相关的展开/折叠部分。当传递 nilindexPath参数时,已经展开的单元格将折叠。
func expandRow(at indexPath: IndexPath?) {
selectedIndexPath = indexPath
tableView.beginUpdates()
tableView.endUpdates()
if let ip = indexPath {
tableView.scrollToRow(at: ip, at: .none, animated: true)
}
}
Run Code Online (Sandbox Code Playgroud)
tableView(_:heightForRowAt:)此方法计算行高并由和调用tableView(_:estimatedHeightForRowAt:):
private func heightForRow(at indexPath: IndexPath) -> CGFloat {
var height: CGFloat …Run Code Online (Sandbox Code Playgroud)