3

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?

Pang
  • 9,564
  • 146
  • 81
  • 122
Carl Pan
  • 99
  • 3
  • 6
  • why are you calling your api inside cellForRowAtIndexPath ?? You can call your api in viewDidLoad() and save the result in array or array of dictionaries as per your requirement.You can then this array to update UI elemnts. – luckyShubhra Sep 22 '17 at 08:14
  • I think it's perfectly normal to call API from cellForRow... because we don't want to fetch external data that the user might never display. – Jonny Jan 25 '18 at 08:29

1 Answers1

2

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.

karthik
  • 621
  • 4
  • 14
  • 1
    I have everything you put here. I think the problem is I set UI in an asynchronous callback from Firebase (I'm using Firebase as backend). So before the actual data is set in background, AutoLayout already deduced the height before the data is obtained. That's why I'm calling reloadrows method to force the cell to reload in-order for the AutoLayout to set constraints based on returned data. Have you met any situation like this before? Thanks for your response! – Carl Pan May 20 '16 at 18:22
  • @carl pan, do you getting any glitches while scrolling up the table view ? – karthik May 20 '16 at 18:29
  • Yes I do see weird bounces when I scroll. – Carl Pan May 21 '16 at 17:03
  • I have fixed by doing some workaround, create one mutableDictionary, then set the indexpath height in that dictionary on willDisplayCell method. Then implement eatimateRowHeight delegate method , there search the indexpath from dictionary if it's there return it , else return UITableViewAutomaticDimension. this workaround fixes the bounce or glitches in table view. – karthik May 21 '16 at 17:30
  • Thanks! I will give a try. – Carl Pan May 21 '16 at 21:03