MaximVW
MaximVW

Reputation: 203

Swift: UITableViewCell rowheight

The question I'm going to ask is already asked a lot but not exactly how I want it.

It's very simple, I want to change the row height of a specific cell and I want to do this in the UITableViewCell Class itself. I tried to call the tableview from my UIViewController:

ViewController().tableView.rowHeight = 100

But then I get an error. So my question is, is it possible to change the rowheight in the tableviewcell class itself? If so, how?

Thanks!

Upvotes: 0

Views: 5470

Answers (1)

Paolo
Paolo

Reputation: 3935

UPDATE: Response to Comment

Start with a flag to keep track of whether the cell should expand or not:

let cellShouldExpand = false

When you want to animate the change in height call the following methods:

cellShouldExpand = true
tableView.beginUpdates()
tableView.endUpdates()

And change heightForRowAt: to be as follows:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  if indexPath == indexPathOfCellYouWantToChange && cellShouldExpand {
    return 100 // the height you want
  } else {
    return UITableViewAutomaticDimension
  }
}

Do the opposite to revert the change (i.e. set cellShouldExpand = false and call the tableview update functions again).

Original Response

If you want all cells to have the same height then you can change the rowHeight, except the correct way to do so is not by doing ViewController()... as that instantiates a new view controller. Instead you can simply refer to the tableview automatically like so:

tableView.rowHeight = 100

or

self.tableView.rowHeight = 100

If you want to have a different row height for a specific cell you need to use UITableViewDelegate. You add the delegate conformance when you declare the view controller at the top of the file:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

and then inside the ViewController class you can implement the following method:

let indexPathOfCellYouWantToChange = IndexPath(row: 2, section: 0)

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  if indexPath == indexPathOfCellYouWantToChange {
    return 100 // the height you want
  } else {
    return UITableViewAutomaticDimension
  }
}

(indexPathOfCellYouWantToChange should be set to the row and section of the cell you're interested in changing the height of)

The Apple documentation has more info on how to properly use this method: https://developer.apple.com/reference/uikit/uitableviewdelegate/1614998-tableview

Discussion

The method allows the delegate to specify rows with varying heights. If this method is implemented, the value it returns overrides the value specified for the rowHeight property of UITableView for the given row. There are performance implications to using tableView(_:heightForRowAt:) instead of the rowHeight property. Every time a table view is displayed, it calls tableView(_:heightForRowAt:) on the delegate for each of its rows, which can result in a significant performance problem with table views having a large number of rows (approximately 1000 or more). See also tableView(_:estimatedHeightForRowAt:).

Upvotes: 7

Related Questions