Reputation: 99
I have successfully made my custom uitableviewcell to work with automatic dimension. In my custom cell, I have a label (used to display captions, can be one line or multilines) and an imageview. Auto-layout is set correctly and I specified estimated row height and set row height to be automatic dimension. My custom cell dynamically changes height based on UILabel text when I used dummy data that are setup in viewDidLoad.
However, I have to make API call inside my custom tableviewcell class, i.e inside cellForRowAtIndexPath method to fetch corresponding data to display in my UILabel and UIImageView. After successfully fetching the data, I update the UI elements. In this case, however, the cell is not changing height dynamically and my UILabel is truncated to one line.
I have tried calling reloadRowsAtIndexPath method, but it is causing weird bounces when I scroll the tableview. I have tried hours, any idea how to make automatic tableview row height to work when after fetching data from API call?
Upvotes: 3
Views: 4545
Reputation: 621
check you have followed these things,
self.tableView.rowHeight = UITableViewAutomaticDimension self.tableView.estimatedRowHeight = 50; //Set this to any value that works for you.
set number of lines of the label to 0 (Most important)
Remove if any height constraint given to the label
on cellForRow while returning a cell, try to do this
cell.contentView.setNeedsLayout() cell.contentView.layoutIfNeeded()
Thats it, it will resize the label according to the string you have given. For more info follow this https://www.sitepoint.com/self-sizing-cells-uitableview-auto-layout/ this has step by step infs.
Upvotes: 2