Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell Automatic Row Height not working after asynchronous call

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?

like image 850
Carl Pan Avatar asked Apr 01 '26 08:04

Carl Pan


1 Answers

check you have followed these things,

  1. self.tableView.rowHeight = UITableViewAutomaticDimension self.tableView.estimatedRowHeight = 50; //Set this to any value that works for you.

  2. set number of lines of the label to 0 (Most important)

  3. Remove if any height constraint given to the label

  4. 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.

like image 104
karthik Avatar answered Apr 02 '26 22:04

karthik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!