user1794349
user1794349

Reputation: 33

resizing UIPopoverController to fit UITableController

I have a UIPopOverController that has a UITableController in it. I init all the elements and want to present the popOver, at this time the UITable does not know its size, so I cant set the size of the UIpopOver

also I'm not sure where else I can set the size of the UIPopOver after the UITable knows its size (I can try hack it inside cellForRowAtIndexPath but this is as ugly as it can get

any ideas ?

I tried to define the didView* methods, but none of them get called maybe because the view is inside the popover

Thanks

Upvotes: 1

Views: 1103

Answers (2)

RegularExpression
RegularExpression

Reputation: 3541

I found this code somewhere on the Internet, I'm not sure where or I would give credit. It works perfectly, though (this is in a UITableViewController subclass). It sets the width of the controller to be a tad wider than the widest line displayed in the table (which, in my case leaves room for a disclosure indicator).

 self = [super initWithStyle:style];
    if (self)
    {
        sortOrders = @[@"Alphabetic",@"User-assigned",@"Randomized"];
        selectedSortOrder = @"Alphabetic";
        //
        //  compute the size of the popover based on maximum width and required height to accommodate all rows
        //
        int rowHeight = [self.tableView.delegate tableView:self.tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        int totalRowHeight = [sortOrders count] * rowHeight;
        CGFloat maxWidth = 0.0f;
        for (NSString *s in sortOrders)
        {
            CGSize labelSize = [s sizeWithFont:[UIFont systemFontOfSize:14.0f]];
            if (labelSize.width > maxWidth)
                maxWidth = labelSize.width;
        }
        self.contentSizeForViewInPopover = CGSizeMake(maxWidth+50.0f, totalRowHeight);
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    }
    return self;
}

Upvotes: 0

Sulthan
Sulthan

Reputation: 130152

Before you show the popover, call [table reloadData]. That will force the table to calculate the size. Then just use [table contentSize].

Also note that you can calculate the size of the table by yourself. If you know the data and the height of rows/headers/footers, it's a simple matter of multiplication & addition.

Upvotes: 2

Related Questions