Nikita Krasnov
Nikita Krasnov

Reputation: 57

Get the row height in the HeightForRowAt

I have a UITableViewController with a custom UITableViewCell. Each cell has 2 labels. When the cell is selected it expands to a fixed value, I do it via tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat . I also have set the rowHeight = UITableViewAutomaticDimension , as some cells have to display multiple lines of text. What I want to achieve is when a cell needs to be expanded I want to add 50 points to its current height. So here's the question, how can I get current height of the cell, when the rowHeight = UITableViewAutomaticDimension is set?

Here's my code for the fixed height for the selected state:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if selectedIndexPath == indexPath {
            return selectedHeight
        }else{

        return tableView.estimatedRowHeight
        }
    }

EDIT: I also need to change it after that, by adding some variable to it.

Upvotes: 0

Views: 1739

Answers (2)

Dave Levy
Dave Levy

Reputation: 1172

Building on HamzaLH's answer you might do something like this...

import UIKit

class TableViewController: UITableViewController {

var selectedRow: Int = 999 {
    didSet {
        tableView.beginUpdates()
        tableView.endUpdates()
    }
}


override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5
}



override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.row == selectedRow {  //assign the selected row when touched
        let thisCell = tableView.cellForRow(at: indexPath)

        if let thisHeight = thisCell?.bounds.height {

            return thisHeight + 50

        }
    }
    return 60 //return a default value in case the cell height is not available
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    selectedRow = indexPath.row
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

     let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)


    cell.detailTextLabel?.text = "test"


    return cell
}

}

I'm animating the expansion of the height with a didSet when a the selectedRow is altered.

Also, don't forget you may still need to connect your dataSource and delegate by dragging the outlets in Interface Builder to your View Controller in the storyboard. After that you still need to add this to ViewDidLoad in your ViewController's swift file. enter image description here

tableView.delegate = self
tableView.dataSource = self

I also have an Outlet declared for the tableView like below, and connected in Interface Builder storyboard.

@IBOutlet weak var tableView: UITableView!

enter image description here

Upvotes: 1

LHIOUI
LHIOUI

Reputation: 3337

You need to get the cell using cellForRow and then get the height of the cell.

let cell = tableView.cellForRow(at: indexPath)
let height = cell.bounds.height

Upvotes: 0

Related Questions