Programmatically adjusting height of individual UITableView cells

I would like to be able to change the height of a certain UITableViewCell after I go through the delegate method calls and so on.


Let's say I have three cells that are all part of a class CustomTableViewCell. The CustomTableViewCell class has a connected IBOutlet to a label I placed in the cell in storyboard. I've set AutoLayout constraints on my label so that it is bound to the edges of the cell's contentView. I can now do stuff with the label in the delegate calls (i.e. set it's text, color, so on).


But I also want to be able to change the height of the label after the delegate is done setting up the tableView. Let's say I have a function setCellHeight(height: Double). I then wnat ot be able to set the height of the first cell in the tableView (tableView.visibleCells[0]) to the height parameter. How can I do this?


I've looked at http s://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithSelf-SizingTableViewCells.html#//apple_ref/doc/uid/TP40010853-CH25-SW1


It seems to be meant for cells that have a fixed size determined in AutoLayout that does not chagne throughout the course of time. I've tried doing this, and then in a function that I call after a three second delay in the viewDidLoad(), I set the label's height to 250. Nothing happens.

Answered by Claude31 in 340171022

Not sure what I propose is the best solution, but it works.


I declare an array to hold the height of rows


    @IBOutlet weak var myTable: UITableView!
    var rowHeights = [Int]()


I set them to their standard value in viewDidLoad


        rowHeights = Array(repeating: 40, count: myTable.numberOfRows(inSection: 0))     // 40 is the standard cell height, by default

I use the func to handle cell height:


    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        return CGFloat(rowHeights[indexPath.row])
    }


Then, when I need to change a cell height (for my test, this is when clicking a button, I change height of cell 1 to 100) ; these 2 lines would be what you implement where you change height


    @IBAction func changeCellHeight(_ sender: UIButton) {
      
        rowHeights[1] = rowHeights[1] == 40 ? 100 : 40
        myTable.reloadData()
    }



Note:

It would be better to post the code than explain what the code is. Much easier to analyze.

Accepted Answer

Not sure what I propose is the best solution, but it works.


I declare an array to hold the height of rows


    @IBOutlet weak var myTable: UITableView!
    var rowHeights = [Int]()


I set them to their standard value in viewDidLoad


        rowHeights = Array(repeating: 40, count: myTable.numberOfRows(inSection: 0))     // 40 is the standard cell height, by default

I use the func to handle cell height:


    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        return CGFloat(rowHeights[indexPath.row])
    }


Then, when I need to change a cell height (for my test, this is when clicking a button, I change height of cell 1 to 100) ; these 2 lines would be what you implement where you change height


    @IBAction func changeCellHeight(_ sender: UIButton) {
      
        rowHeights[1] = rowHeights[1] == 40 ? 100 : 40
        myTable.reloadData()
    }



Note:

It would be better to post the code than explain what the code is. Much easier to analyze.

Programmatically adjusting height of individual UITableView cells
 
 
Q