2

I have a table view with a dynamic height of cells. Above the table is menu with buttons. When I click on the menu button, the data is loaded into the table. When data is loaded, I want to have an animation in a cell that changes the height of a cell. I wonder how this can be done?

enter image description here

Thanks for the help.

Mark
  • 159
  • 1
  • 10

1 Answers1

1

Swift 4

Create a boolean variable in your model for checking if your cell is expanded or not. if you want to expand the cell as long as your label height you should connect you labels constraint to your contentView in all directions and set number of lines to 0 in your storyboard.

 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
            }
    }
}

Also you can place this two lines in your viewDidLoad() function:

detailTableView.estimatedRowHeight = 60
detailTableView.rowHeight = UITableViewAutomaticDimension
Ahmadreza
  • 6,950
  • 5
  • 50
  • 69