0

I'm using this algorithm to have a multiline label in a cell. I want to present the table view in a popover. Therefore I set the preferredContentSize similar to here in viewDidAppear.

But if I query the contentSize of the tableview I get the wrong height. It seems that it always takes the default height of the row (44f) despite my last cell has a dynamic height (depending on the text in the multiline label).

What I've noted is that viewDidAppear is called before the calculation of the height of all cells has been finished. What does that mean? Here is the excerpt of the index paths which are queried in heightForRowAtIndexPath:

2015-11-13 14:49:20.592 [53865:2230330] Default: <NSIndexPath: 0x7eb27130> {length = 2, path = 0 - 0}
2015-11-13 14:49:20.594 [53865:2230330] Default: <NSIndexPath: 0x7e904750> {length = 2, path = 0 - 1}
2015-11-13 14:49:20.595 [53865:2230330] Default: <NSIndexPath: 0x7e9f7730> {length = 2, path = 0 - 2}
2015-11-13 14:49:20.596 [53865:2230330] Default: <NSIndexPath: 0x7e9fbd60> {length = 2, path = 0 - 3}
2015-11-13 14:49:20.596 [53865:2230330] Default: <NSIndexPath: 0x7e91bee0> {length = 2, path = 1 - 0}
2015-11-13 14:49:20.597 [53865:2230330] Default: <NSIndexPath: 0x7e90bde0> {length = 2, path = 1 - 1}
2015-11-13 14:49:20.598 [53865:2230330] Default: <NSIndexPath: 0x7e9081e0> {length = 2, path = 1 - 2}
2015-11-13 14:49:20.599 [53865:2230330] Default: <NSIndexPath: 0x7e902d30> {length = 2, path = 1 - 3}
2015-11-13 14:49:20.599 [53865:2230330] Default: <NSIndexPath: 0x7e900b10> {length = 2, path = 1 - 4}
2015-11-13 14:49:20.600 [53865:2230330] Default: <NSIndexPath: 0x7ec24470> {length = 2, path = 0 - 0}
2015-11-13 14:49:20.601 [53865:2230330] Default: <NSIndexPath: 0x7ec24590> {length = 2, path = 0 - 1}
2015-11-13 14:49:20.601 [53865:2230330] Default: <NSIndexPath: 0x7ec24570> {length = 2, path = 0 - 2}
2015-11-13 14:49:20.601 [53865:2230330] Default: <NSIndexPath: 0x7ec24580> {length = 2, path = 0 - 3}
2015-11-13 14:49:20.601 [53865:2230330] Default: <NSIndexPath: 0x7ec24a60> {length = 2, path = 1 - 0}
2015-11-13 14:49:20.601 [53865:2230330] Default: <NSIndexPath: 0x7ec24bf0> {length = 2, path = 1 - 1}
2015-11-13 14:49:20.602 [53865:2230330] Default: <NSIndexPath: 0x7ec24d80> {length = 2, path = 1 - 2}
2015-11-13 14:49:20.602 [53865:2230330] Default: <NSIndexPath: 0x7ec24f10> {length = 2, path = 1 - 3}
2015-11-13 14:49:20.602 [53865:2230330] Default: <NSIndexPath: 0x7ec250a0> {length = 2, path = 1 - 4}
// viewDidAppear is called setting the preferredContentSize
2015-11-13 14:49:31.374 [53865:2230330] Default: <NSIndexPath: 0x7e80fb20> {length = 2, path = 2 - 0}
2015-11-13 14:49:31.378 [53865:2230330] Adaptive: <NSIndexPath: 0x7e9088b0> {length = 2, path = 3 - 0}
2015-11-13 14:49:31.379 [53865:2230330] Default: <NSIndexPath: 0x7ec231c0> {length = 2, path = 2 - 0}

How can I set the correct preferredContentSize?

Community
  • 1
  • 1
testing
  • 19,681
  • 50
  • 236
  • 417

1 Answers1

0

Don't know why, but it seems that estimatedHeightForRowAtIndexPath has an influence on this.

If I get the index path for my dynamic cell I calculate the cell height and use that for the estimation. My code in C# looks like this:

public override nfloat EstimatedHeight(UITableView tableView, NSIndexPath indexPath)
{
    if (indexPath.Section == 3)
    {
        return this.GetHeightForRow(tableView, indexPath);
    }

    return UITableView.AutomaticDimension;
}

This seems to work, but I still think this is kind of a hack ...

testing
  • 19,681
  • 50
  • 236
  • 417