unable to Set Height of the CustomCells in swift

86 Views Asked by At

I have many customCells each cell may have a button which acts like a textbox or textArea.When the View loads they have very less height.

override func viewDidLoad() {

    super.viewDidLoad();

    self.view.backgroundColor = UIColor.groupTableViewBackground
    self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
   self.tableView.estimatedRowHeight = 100
   tableView.rowHeight = UITableViewAutomaticDimension
   self.tableView.setNeedsLayout()
   self.tableView.layoutIfNeeded()

}

And i have the following constraints defined on the cell enter image description here

Will auto layout work with prototype cells at all? If not how to implement this? I tried with HeightforrowAtIndexPath method layout looks good only after scroll. Initial load of the view will not come with good layout.

below is my code for this.

  func tableView(_ tableView: UITableView, heightForRowAt indexPath:   IndexPath) -> CGFloat {
   if (eleData.elementType=="apple")
    {
        return 90;
   }  
 else if (eleData.elementType=="banana")
    {
        return 50;
   }     
else  
{
  return 30;
}

}

Any suggestions . Thank you in advance.

1

There are 1 best solutions below

5
On BEST ANSWER

Quick google search gives you your answer...

UITableView dynamic cell heights only correct after some scrolling

You need to add layout if needed before returning your cell in the cellForRowAtIndexPath method:

cell.layoutIfNeeded()