I want my last cell to take all the remaining space left in the tableview. So I wrote this piece of code:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == tableView.numberOfRows(inSection: 0) - 1 {
let sectionheight: CGRect = tableView.rect(forSection: 0)
let tableViewFrame: CGRect = tableView.frame
let height: CGFloat = tableViewFrame.size.height - (sectionheight.size.height - 100)
cell.frame.size.height = height
cell.layoutIfNeeded()
}
}
and in the heightForRow for that cell I return just a value of like 100.
The problem is that the height calculated is correct but the cell doesn't change.
How can I make sure the cells height is changed?
You should do height calculation in heightForRow
, not in willDisplayCell
, you can also save some property first in willDisplayCell
then call tableView.beginUpdate
and endUpdate
to force recalculation cell height
The cell doesnt change because the tableview handle cell height, not the cell, the cell only control it's content height