Dynamic Cell Heights

Currently, the cells have a fixed height of 65 points. It is much better to allow the content of the cell to drive its height. That way, if the content ever changes, the table view cell’s height can change automatically.

You can achieve this goal, as you have probably guessed, with Auto Layout. The UITableViewCell needs to have vertical constraints that will exactly determine the height of the cell. Currently, ItemCell does not have sufficient constraints for this. You need to add a constraint between the two left labels that fixes the vertical spacing between them.

Open Main.storyboard. Control-drag from the nameLabel to the serialNumberLabel and select Vertical Spacing.

Now open ItemsViewController.swift ...

Get iOS Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.