Junior — Middle
How can you dynamically determine the height of each cell in an interface?
sobes.tech AI
Answer from AI
In iOS, to dynamically determine the height of a cell in a table (UITableView) or collection (UICollectionView), you can use automatic height calculation based on the content.
For UITableView:
- Set the property
tableView.rowHeight = UITableView.automaticDimension - Set
tableView.estimatedRowHeightto an approximate value - Configure constraints within the cell so that Auto Layout can calculate the height
Example in Swift:
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 100
For UICollectionView, you can use UICollectionViewFlowLayout with estimatedItemSize or create a custom layout.
Thus, the cell height will be calculated automatically depending on the content and Auto Layout constraints.