UIKit Collection View inside TableViewCell

I have a collelctionview inside a tableview cell in UIKit. The cells of the collectionview have dynamic content and their height and width depends on the content, which decides the height of the collectionview inside tableview cell. I have used many methods to calculate height correctly at first and as I scroll the tableview or perform any button action, the height does not gets correctly to me. Please provide a good solution or replacement for this problem.