Swift 具有调整单元格大小动画的动态单元格大小
我有一个具有动态单元格高度的表格视图。桌子上方是带按钮的菜单。当我点击菜单按钮时,数据被加载到表中。加载数据时,我希望在单元格中有一个动画,可以更改单元格的高度。我想知道如何才能做到这一点Swift 具有调整单元格大小动画的动态单元格大小,swift,uitableview,swift3,uiviewanimation,Swift,Uitableview,Swift3,Uiviewanimation,我有一个具有动态单元格高度的表格视图。桌子上方是带按钮的菜单。当我点击菜单按钮时,数据被加载到表中。加载数据时,我希望在单元格中有一个动画,可以更改单元格的高度。我想知道如何才能做到这一点 谢谢你的帮助。Swift 4 在模型中创建一个布尔变量,用于检查单元格是否展开。 如果要将单元格扩展到标签高度,则应将标签约束连接到contentView的各个方向,并在情节提要中将行数设置为0 func tableView(_ tableView: UITableView, didSelectRowAt
谢谢你的帮助。Swift 4 在模型中创建一个布尔变量,用于检查单元格是否展开。 如果要将单元格扩展到标签高度,则应将标签约束连接到contentView的各个方向,并在情节提要中将行数设置为0
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
details[indexPath.row].cellIsOpen = !details[indexPath.row].cellIsOpen
detailTableView.reloadData()
detailTableView.beginUpdates()
detailTableView.endUpdates()
detailViewHeightConstraint.constant = CGFloat(detailTableView.contentSize.height) // the height of whole tableView
UIView.animate(withDuration: 0.3) {
self.view.layoutIfNeeded()
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if details[indexPath.row].cellIsOpen {
return UITableViewAutomaticDimension // or any number you wish
} else {
return 60 // default closed cell height
}
}
}
也可以在viewDidLoad()函数中放置这两行:
detailTableView.estimatedRowHeight = 60
detailTableView.rowHeight = UITableViewAutomaticDimension