iosswiftuitableviewnstableviewcell

Cannot change the row height of TableViewCells - Swift


I am attempting to set the height of a custom tableViewCell, but the code below does not work. What is the better way to do this?

Programmatically:

override func viewDidLoad() {
  super.viewDidLoad()
  self.tableView.rowHeight = 140
}

via storyboard:

  1. Selected on tableView -> size Inspector -> set row height to 140.

I have also tried the following:

  1. Selected the custom UITableViewCell -> size Inspector -> set row height to 140.

Solution

  • if you are using UITableViewController like this:

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 140.0
    }
    

    if you are using UIViewController, you must implement UITableViewDelegate

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