Wednesday, December 13, 2017

iOS: set different row height for different prototype cells in a single table view

Since IOS 8.1,  supporting different height for different row prototype is quite easy.

First in storyboard, setting height constraint for different row prototype, then adding those rows into a single UITableView will show the height constraints are ignored at runtime and the row height is always using the row height configured in TableView row height property.

In order to apply the row height constraint defined in row prototype, add the below code in the viewController's viewDidLoad method

        tableView.estimatedRowHeight = 44

        tableView.rowHeight = UITableViewAutomaticDimension

The estimatedRowHeight can be set to the same value as configured in tableview's row height property.

Note, inside the cell, the vertical constraint must set to require the vertical cell height. For example, if a button has top and bottom and height constraints added as the cell height, it will work. But if the button only has top and height constraints, then it will not work, as the cell will be shrunked to estimated row height.

No comments:

Post a Comment