5

I am using UITableViewAutomaticDimension to calculate height for my table view cell. It works fine for me. But for some cells, the height returned is 44 rather than dynamically calculating height. But on scrolling up an looking back to the same cell, the height is recalculated perfectly. So I guess I do not have any faulty constraints because, after I scroll and correct all cell heights manually, no constraint breaking warning are shown afterwards.

Edit

The below given is the screenshot of image(got messed up when cropped, but can see the issue from the profile image.). The first cell height is calculated correctly. But the rest are faulty. enter image description here

Community
  • 1
  • 1
krishnanunni
  • 510
  • 7
  • 16
  • 1
    try reloading the tableview in view did appear. – Sujith Chandran Sep 21 '15 at 06:39
  • @SujithChandran - Initially I had issue that none of the cells get to correct height. Then I reloaded my tableview in viewdidappear. Then initially visible cells gets correct height. But some cells, say cell position 6-8 of a 10 row table, doesn't calculate height correctly. But on cell reuse the issue is corrected. – krishnanunni Sep 21 '15 at 06:43
  • This may help you:- http://www.programmingcrew.in/2015/09/uitable-view-cell-dynamic-height-ios7.html?view=flipcard – pkc456 Oct 16 '15 at 12:16

2 Answers2

2

I have added the following and everything worked fine.

(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}
krishnanunni
  • 510
  • 7
  • 16
  • 1
    You should return an actual number in the `estimatedHeight` method. Don't return automaticDiemnsion here. It is used as a rough first pass for laying out the scroll view. If your cells are a minimum of 100 points high then return 100. – Fogmeister Sep 21 '15 at 08:21
  • 1
    @Fogmeister - The above code worked for me. I have previously given 500 as the estimatedHeight. But I got cells with height 44 and breaking my constraints. So if I can not write automatic dimension to estimatedheight, what am I suppose to do to solve the issue? Please suggest another solution. – krishnanunni Sep 21 '15 at 08:30
  • The estimated height can be any number. It won't affect the actual cell height as long as you have set up the constraints properly. Like I said, this is a number to give the table view an idea of how big the content is. – Fogmeister Sep 21 '15 at 08:34
  • 1
    @Fogmeister - I understand. But when I put the estimated row height to any value(I had used 500 previously), I am experiencing wrong cell height calculated by iOS. But when I returned the automatic dimension(this is the only modification I have made) to `estimatedHeightForRowAtIndexPath:`, the height calculation is working fine. – krishnanunni Sep 21 '15 at 08:38
  • 1
    This worked for me, debugging this issue with Eureka cells using XIBs. Spent over 30 hours debugging this. Your answer makes no sense, but it worked. – Andrew Plummer May 09 '18 at 03:13
0

In my case it was something wrong with layoutMarginsGuide top and bottom anchor. I use it for cell subviews and set preservesSuperviewLayoutMargins = true for its subviews. This caused the mistake when system tried use systemLayoutSizeFitting to calculate cells height first time.

Roman Filippov
  • 2,289
  • 2
  • 11
  • 17