我有一个具有动态单元格高度的表格 View 。表格上方是带按钮的菜单。当我点击菜单按钮时,数据被加载到表中。加载数据时,我希望在单元格中有一个动画来改变单元格的高度。我想知道如何做到这一点?
感谢您的帮助。
最佳答案
swift 4
在您的模型中创建一个 bool 变量,用于检查您的单元格是否展开。 如果您想将单元格扩展到与标签高度一样长,您应该将标签约束连接到各个方向的 contentView,并在 Storyboard中将行数设置为 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
关于swift - 具有调整单元格动画大小的动态单元格大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48253073/