Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 766
Code Wiget Avatar asked Feb 02 '17 15:02

Code Wiget


2 Answers

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
}
like image 170
rmaddy Avatar answered Sep 27 '22 20:09

rmaddy


For me, I had to do this.

self.tableviewObj.delegate = self

So, don't forget to add delegate to self. Because heightForRowAtIndexparth is declared in delegate protocol.

like image 29
Narasimha Nallamsetty Avatar answered Sep 27 '22 19:09

Narasimha Nallamsetty