Dynamic UITableView height in UIPopoverController

2020-02-20 04:03发布

I have an ipad popover that contains an UITableView. After the table is populated, it usually has a few items in it only (4-5), so I'm looking for a way to resize the popover (contentSizeForViewInPopover) to the actual table height (the summed height of all its cells).

So, I do have the height but I'm not sure where to call contentSizeForViewInPopover, I did try to call it in: viewDidAppear and viewWillAppear but with no success since it seems that the table gets populated later and the actual height is only available later.

Any thoughts on this? Thanks!

EDIT: My cells have different heights based on the content they carry, I can't pre-calculate the height with noOfRows * cellHeight.

7条回答
别忘想泡老子
2楼-- · 2020-02-20 05:08

This answer is courtesy of @BenLings from one of the comments above. It's a really clean solution and worth having its own answer:

- (CGSize)contentSizeForViewInPopover {
    // Currently no way to obtain the width dynamically before viewWillAppear.
    CGFloat width = 200.0; 
    CGRect rect = [self.tableView rectForSection:[self.tableView numberOfSections] - 1];
    CGFloat height = CGRectGetMaxY(rect);
    return (CGSize){width, height};
}
查看更多
登录 后发表回答