Swift动画UITableViewCell扩展高度约束

时间:2018-02-18 17:48:16

标签: ios swift animation tableview constraints

我有一个带有静态单元格的TableView,我想在按下按钮时展开它。单元格内部有一个容器视图约束已正确设置,但我不确定如何实际动画扩展单元格,因为它需要我刷新< strong>表格视图更新约束并展开单元格。

目前,当我致电<script type="text/javascript" src="https://code.jquery.com/jquery-latest.js"></script> 时,它没有动画,因为我正在呼叫expandContainerView

这是我用来扩展单元格的代码

self.tableView.reloadData

这是索引代码

中每行的高度
@objc private func expandContainerView(notification: NSNotification) {

    self.view.layoutIfNeeded()

    UIView.animate(withDuration: 2.0, delay: 0.0, options: .curveEaseOut, animations: {
        self.containerHeightConstraint.constant = 410
        self.view.layoutIfNeeded()
        self.tableView.reloadData()
    }, completion: nil)
}

2 个答案:

答案 0 :(得分:2)

您只能使用以下代码重新加载要扩展的单元格。我添加了didSelectRowAt,但您可以在按钮操作方法中添加相同的代码。 将expandCell变量设置为true,以便在重新加载时更改单元格的高度。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

     // Expand View 
     self.expandCell = true 
     self.tableView.beginUpdates()
     self.tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
     self.tableView.endUpdates()
}

您需要指定高度以展开单元格视图,否则它将显示默认高度。

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.row == expandRowIndex && self.expandCell {
         return 200
    }
    return UITableViewAutomaticDimension
} 

注意:不需要动画。重新加载单元格时扩展视图动画

答案 1 :(得分:0)

尝试没有动画块:

containerHeightConstraint.constant = 410
tableView.beginUpdates()
tableView.endUpdates()
相关问题