【发布时间】:2018-01-14 18:59:04
【问题描述】:
我有一个具有动态单元格高度的表格视图。桌子上方是带有按钮的菜单。当我单击菜单按钮时,数据被加载到表中。加载数据时,我想在一个单元格中有一个动画来改变单元格的高度。我想知道如何做到这一点?
感谢您的帮助。
【问题讨论】:
标签: swift uitableview swift3 uiviewanimation
我有一个具有动态单元格高度的表格视图。桌子上方是带有按钮的菜单。当我单击菜单按钮时,数据被加载到表中。加载数据时,我想在一个单元格中有一个动画来改变单元格的高度。我想知道如何做到这一点?
感谢您的帮助。
【问题讨论】:
标签: swift uitableview swift3 uiviewanimation
斯威夫特 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
【讨论】: