我想在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
上的屏幕答案 0 :(得分:2)
我相信您需要在动画调用的外部设置cell.expandViewHeight.constant = 40
,而在内部调用self.layoutIfNeeded()
。像这样:
cell.expandViewHeight.constant = 40
UIView.animate(withDuration: 0.5) {
self.layoutIfNeeded()
}
答案 1 :(得分:0)
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()
}
}
}