Neil Johnson Neil Johnson - 2 months ago 15
iOS Question

Xcode Table View Cells won't resize height

I am trying to have a

UITableView
with different sized prototype cells.

I set the row height in my Table View to 243. The first and second cells' individual row height are set to 243 and 465, respectively. When I run my app, both cells appear at 243 height.

Here is what I have tried:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 25
}
//does not change cell height


And:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 25
}
//Method does not override any method from its superclass


How can I configure my project to successfully resize the second cell?

Answer

The signature of the delegate method has changed in Swift 3. It needs to be:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.row == 1 {
        return 465.0
    } else {
        return 243.0
    }
}

With the wrong signature the table view doesn't know you've implemented the method so it never gets called.