我需要扩展或折叠表视图单元格及其内容。为此,我正在使用NSLayoutConstraints。尽管可以完成工作,但单元格中的其他视图仍面临一些令人讨厌的布局问题。Here is a video.这是我的代码:
extension ViewController: TableViewCellDelegate {
func tableView(shouldExpand cell: TableViewCell) {
tableView.performBatchUpdates({
cell.expand()
})
}
func tableView(shouldCollapse cell: TableViewCell) {
tableView.performBatchUpdates({
cell.collapse()
})
}
}
func expand() {
UIView.animate(withDuration: 0.57) {
self.viewHeight.constant = 320
self.view.alpha = 1
self.layoutIfNeeded()
}
}
func collapse() {
UIView.animate(withDuration: 0.57) {
self.viewHeight.constant = 0
self.view.alpha = 0
self.layoutIfNeeded()
}
}
如何停止上视图停止调整大小?
performBatchUpdates用于根据数据源中的更改为tableView
中的更改制作动画。由于您在数据源中没有任何更改,因此无需在tableView.performBatchUpdates
块内进行动画处理。不使用performBatchUpdates
即可尝试。
func tableView(shouldExpand cell: TableViewCell) {
cell.expand()
}
func tableView(shouldCollapse cell: TableViewCell) {
cell.collapse()
}