Search code examples
iosswiftuitableviewheightforrowatindexpath

TableView - heightForRowAtIndexPath not being called


After going through all of the other Stack Overflow forms, I have implemented a dynamic height for one of my cells as follows:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell

    if(indexPath.row == 1){
        var image : UIImage = maskRoundedImage(image: UIImage(named: "Temp_Profile.png")!, radius: 150)
        cell.imageView?.image = image
        return cell
    }
    cell.textLabel?.text = TableArray[indexPath.row]
    cell.backgroundColor = UIColor.init(colorLiteralRed: 0.56, green: 0, blue: 0.035, alpha: 1)
    cell.textLabel?.textColor = UIColor.white
    cell.textLabel?.font = UIFont(name: "Helvetica", size: 28)

    return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if  indexPath.row == 1 {
        return UITableViewAutomaticDimension * 3

    }
    return UITableViewAutomaticDimension
}

It seems pretty straightforward, but a debugging session is showing that heightForRowAtIndexPath is never being called. The View Controller is a UITableViewController and everything else works properly. Do any of you see any reason that this function is not being called?

Thank you for the help!


Solution

  • In Swift 3, the signature is:

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    

    You have the old signature so it isn't recognized.

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 1 {
            return UITableViewAutomaticDimension * 3
    
        }
        return UITableViewAutomaticDimension
    }