Reputation: 203
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
Reputation: 3935
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).
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 ofUITableView
for the given row. There are performance implications to usingtableView(_:heightForRowAt:)
instead of therowHeight
property. Every time a table view is displayed, it callstableView(_: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 alsotableView(_:estimatedHeightForRowAt:)
.
Upvotes: 7