0

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!

MaximVW
  • 203
  • 1
  • 4
  • 12
  • you can't, there can be some workarounds, but you should never do something like that. – JuicyFruit Apr 18 '17 at 17:07
  • do you want different cellHeight for different cell classes? – Mat Apr 18 '17 at 17:08
  • Since you say the question *"is already asked a lot"*, the better question is: Why do you want to do that? If you describe what you are *trying to do*, rather than what "code" you want to use, you're much more likely to get good help (or you might even answer your own question). – DonMag Apr 18 '17 at 17:31
  • So in my cell, there is a drawing (rectangle) and 1 line will go open (the bottom line) , it will rotate a little bit down. But my cells height is only the height of the rectangle and I want to animate the height of that specific cell when the bottom line goes open (down). – MaximVW Apr 18 '17 at 18:08
  • Sounds like a job for `UITableViewAutomaticDimension` - Take a look here: http://stackoverflow.com/questions/43096231/expand-uilabel-inside-uitableview-with-more-button-like-instagram – DonMag Apr 18 '17 at 18:41

1 Answers1

7

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:).

Paolo
  • 3,825
  • 4
  • 25
  • 41
  • Thanks that helped already a little bit but have a look at my response to my question, I have told more about what I want. It would be really awesome if you could help me out with that. But already a big thank you ! – MaximVW Apr 18 '17 at 18:09
  • @MaximVW I've updated my response, let me know if this answers your question – Paolo Apr 18 '17 at 18:30