Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static UITableView, make a single cell with dynamic height in Swift

I have a static UITableView which has 12 rows and for 11 rows I know what the height needs to be.

I have a UILabel which has dynamic text which sits inside the 12 row, how do i go about making just that one cell to have a dynamic height based on the text in the UILabel.

I have tried the below code inside viewDidLoad, but it didn't work. The rows remained the same height. I have also set the lines = 0 for the UILabel

    tableView.estimatedRowHeight = 100.0
    tableView.rowHeight = UITableViewAutomaticDimension
like image 331
Mugunthan Balakrishnan Avatar asked Oct 06 '15 11:10

Mugunthan Balakrishnan


People also ask

What is Indexpath in tableView Swift?

Swift version: 5.6. Index paths describe an item's position inside a table view or collection view, storing both its section and its position inside that section.


1 Answers

Have you tried this?

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if (indexPath.row < 11) {
        // Everything starts at 0, so this covers 0-10
        // Whatever your cell height is
        return <#fixedCellHeight#>
    } else {
        // This is your 12th cell (indexPath.row 11), as we start counting at 0
        return UITableViewAutomaticDimension
    }
}
like image 74
Adrian Avatar answered Sep 17 '22 12:09

Adrian