0

I am a swift/xcode newbie, following a book app that demonstrates TableViews with prototype cells using Auto Layout. The book provides completed sample projects, so I have a project to compare to.

The table view cells use images of different heights. I followed the book exactly, used the same images, etc. But the book project displays TableViewCell rows that dynamically adjust to the height of the images, but my project does not.

    // both of the projects contain this line
    tableView.estimatedRowHeight = 50

    // I added this line to get cell rows to dynamically adjust size
    tableView.rowHeight = UITableViewAutomaticDimension

With the magic line of code added, my project works perfectly, just like the book project.

My question is why does the book project display properly without the magic line of UITableViewAutomaticDimension code? Is there an attribute setting somewhere that I might have missed? Is there some other way the table view can know to use automatic dimensions?

I have carefully checked all my constraints (they follow the book), all the attributes and sizes in the inspectors for table views, cells, frames, imageviews, and have scoured the net for possible answers. (Which is how I found out about the automatic dimension code magic.)

Does anyone know where else (other than the magic code line) xcode can be told to use automatic dimensions on cell rows? Any suggestions on where I might look to find out why two apparently identical projects product different cell row height sizing behaviours? Thanks

Kevin
  • 1,548
  • 2
  • 19
  • 34
  • look into this http://stackoverflow.com/a/25888127/2963912 – techloverr Mar 04 '16 at 06:09
  • Thanks techloverr, your reference seems relevant. I was about to post my "solution" when I saw your comment. I think it's relevant to the answer that I'm about to post. – Kevin Mar 04 '16 at 06:15

2 Answers2

0

I redid the whole project over again, and now it works properly without the magic line of code. @techloverr contributed a reference here on stackoverflow that (I think) supports my theory of what is going on.

My answer theory is that the default value for cells is automatic dimensioning. This is supported by the reference above.

I think during the first project attempt, I must have manually bumped or adjusted the size of the prototype table cell while loading in the image view or label placeholders.

My theory is that this tiny change caused xcode to remove the default setting and use the new (fixed) cell row height that I accidentally created. The magic code line in turn overrode the new fixed-size "default" setting.

When I redid the whole project from scratch, I took special care to NOT bump the size of the prototype cell in any way. I used all the same constraints, code, etc. And the project built and displayed properly.

Hopefully someone else can benefit from this experience. The xcode IB is touchy!

Community
  • 1
  • 1
Kevin
  • 1,548
  • 2
  • 19
  • 34
0

I have tried playing with my code to see how the tableView's cell auto layouts work. I have had these two delegates in my project to auto resize my tableView cell height,

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 44.0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return UITableViewAutomaticDimension;
}

And it was working as expected, then i just commented these two delegates and put this line in my viewDidLoad method,

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.estimatedRowHeight = 50;
}

And still it is working as previously.

So i dig a bit into this and i found out that, many developers have had reported about this similar issue you are facing, self.tableView.rowHeight = UITableViewAutomaticDimension; some time behaves weirdly.

Some times it works by just adding

self.tableView.estimatedRowHeight = 50;

(as mine is now), some times you have to add both

tableView.estimatedRowHeight = 50

tableView.rowHeight = UITableViewAutomaticDimension

And in some cases none of the above work but the two delegates that is estimatedHeightForRowAtIndexPath and heightForRowAtIndexPath.

This is what i know so far about UITableViewAutomaticDimension, if you find some other info, do share.

Thanks!

Bharat Modi
  • 4,158
  • 2
  • 16
  • 27