我想在UITableView
单元格中使用动画扩展视图高度。它正在工作,但动画不能按我的意愿工作。我的代码是这样的。
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let cell = listingTableView.dequeueReusableCell(withIdentifier: "listingTableViewCell", for: indexPath) as! listingTableViewCell
//Charactristic expand
let isExpand = isExpandViewArr[indexPath.row]
if isExpand == true {
cell.expandBtn.setImage(UIImage(named: "down-arrow"), for: .normal)
UIView.animate(withDuration: 0.5) {
cell.expandViewHeight.constant = 0
self.loadViewIfNeeded()
}
}else{
cell.expandBtn.setImage(UIImage(named: "up-arrow"), for: .normal)
UIView.animate(withDuration: 0.5) {
cell.expandViewHeight.constant = 40
self.loadViewIfNeeded()
}
}
}
请检查链接屏幕:https://ibb.co/XjjXRz5
我相信你需要在动画调用之外设置cell.expandViewHeight.constant = 40
并简单地在里面调用self.layoutIfNeeded()
。像这样:
cell.expandViewHeight.constant = 40
UIView.animate(withDuration: 0.5) {
self.layoutIfNeeded()
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let cell = listingTableView.dequeueReusableCell(withIdentifier: "listingTableViewCell", for: indexPath) as! listingTableViewCell
//Charactristic expand
let isExpand = isExpandViewArr[indexPath.row]
if isExpand == true {
cell.expandBtn.setImage(UIImage(named: "down-arrow"), for: .normal)
DispatchQueue.main.async {
self.tblView.beginUpdates()
cell.expandViewHeight.constant = 0
self.tblView.endUpdates()
}
}else{
cell.expandBtn.setImage(UIImage(named: "down-arrow"), for: .normal)
DispatchQueue.main.async {
self.tblView.beginUpdates()
cell.expandViewHeight.constant = 40
self.tblView.endUpdates()
}
}
}