Dynamic UITableView height in UIPopoverController (contentSizeForViewInPopover)?

假如想象 提交于 2019-11-30 02:24:51

I wanted to change the contentSizeForViewInPopover when my view appeared to match the UITableView and also when I call reloadData as I only call this when removing or adding rows or sections.

The following method calculates the correct height and width and sets the contentSizeForViewInPopover

-(void) reloadViewHeight
{
    float currentTotal = 0;

    //Need to total each section
    for (int i = 0; i < [self.tableView numberOfSections]; i++) 
    {
        CGRect sectionRect = [self.tableView rectForSection:i];
        currentTotal += sectionRect.size.height;
    }

    //Set the contentSizeForViewInPopover
    self.contentSizeForViewInPopover = CGSizeMake(self.tableView.frame.size.width, currentTotal);
}
memmons

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};
}

Indirect Approach :

Set your custom height for your UITableViewCell using

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [indexPath row] * 40;
}

Find the total number of rows at a point of your program ... Using the numberOfRowsInSection, get the number of rows.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

return [self.yourArray count];

//yourArray is the array with which you are populating the tableView
}

If you have more than one section, multiple it with the result for the number of rows for the effective number of rows.

Now

UITableView Actual Height = Height of UITableViewCell * Total number of rows.

Updated Answer:

If the cell sizes vary, you might have to do do one of these:

  1. Force the text to a smaller size so that all cells have equal height... This can be done using

    'sizeToFit'

  2. You will have to find the height of the text using another function. Something like...

    • (float)calculatedHeight { return textLabel.frame.origin.ytextLabel.frame.size.height5; }
  3. You can look at THIS tutorial for resizing UITableViewCell for variable text.

  4. https://discussions.apple.com/thread/1525150?start=0&tstart=0

I did it like this from controller containing a table:

- (void)updatePopoverContentSize {
    CGSize size = { CGFLOAT_MAX, CGFLOAT_MAX };
    size = [self.tableView sizeThatFits: size];
    size.width = 320; // some hard-coded value table doesn't return anything useful for width 
    size.hight = MIN(size.hight, 400); // make sure it is not too big.

    self.contentSizeForViewInPopover = size;
}

There's no need to create an artificial hook, this works well (on iOS 7 at least):

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    CGSize size = self.view.bounds.size;
    size.height = fmaxf(size.height, self.tableView.contentSize.height);
    self.preferredContentSize = size;
}

This should do the trick

override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()

        self.preferredContentSize = CGSizeMake(0, self.tableView.contentSize.height)
 }

An alternate way to get the height is to add the UITableViewController's view with an alpha set to 0.0 to the view hierarchy and then get the content size using the contentSize property of the table view. This is possible as the table view is a subclass of the scroll view.

Once you have the content size, set the value of contentSizeForViewInPopover and then push it onto the popover.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!