Search code examples
iosuitableviewuipopovercontroller

Get correct height of tableview in popover after an additional cell is shown/hidden


I'm displaying an UITableView in a UIPopoverController. This table view hides/shows some cells if another cell is tapped (compare with this tutorial). Because it is in a popover I want to adapt the size of the popover. Therefore I want to use an animation with preferredContentSize.

The problem now is that if I calculate the contentSize I don't get the actual size, because heightForRowAtIndexPath is called afterwards. I need the table view height after the table view has been reloaded or detect the change in height of the table view.

How can I get the correct height of the table view to adapt the size of the popover?

Possible workarounds:

  1. scroll at the position of the new cell
  2. never hides the cells
  3. show popover in full size
  4. manually calculate the sizes

But all workarounds has some disadvantages/limitations. No. 1 doesn't looks nice, No. 2 doesn't work well for me in landscape mode (other content can be overlooked), No. 3 doesn't looks nice and also other content can be overlooked and No. 4 would work if I'm aware of all possible cases.

Still looking for a better solution.


Solution

  • Only manually calculating the height did it for me. My code is in C# and this is what I did:

    I used a class variable where I stored the actual height. In viewDidLoad I have this

    TableView.LayoutIfNeeded ();
    height = TableView.ContentSize.Height;
    this.PreferredContentSize = new SizeF (320f, height);
    

    In showDatePickerCell and in hideDatePickerCell I calculate the new height and set it for the popover:

    UIView.Animate (
        0.25,
        animation: () => {
            height += 206f;
            this.PreferredContentSize = new SizeF (320f, height);
        }
    );
    

    Now the popover expands and shrinks as desired.