动态单元格大小,调整大小单元格动画

时间:2018-01-14 18:59:05

标签: swift uitableview swift3 uiviewanimation

我有一个动态高度为单元格的表视图。桌子上方是带按钮的菜单。当我单击菜单按钮时,数据将加载到表中。加载数据时,我希望在单元格中有一个动画来改变单元格的高度。我想知道如何做到这一点?

enter image description here

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

快捷键4

在模型中创建一个布尔变量,以检查单元格是否已扩展。 如果要扩展单元格,只要标签高度就可以在所有方向上将标签约束连接到contentView,并在情节提要中将行数设置为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
相关问题