调整UIPopoverController的大小以适应UITableController

时间:2013-08-13 19:52:29

标签: ios uitableview uipopovercontroller

我有一个UIPopOverController,里面有一个UITableController。 我初始化所有元素并想要呈现popOver,此时UITable不知道它的大小,所以我无法设置UIpopOver的大小

我也不确定在UITable知道它的大小之后我还可以在哪里设置UIPopOver的大小(我可以在cellForRowAtIndexPath中尝试破解它,但这样可以得到它的丑陋

任何想法?

我试图定义didView *方法,但是没有一个被调用,可能是因为视图在popover内部

由于

2 个答案:

答案 0 :(得分:2)

在显示弹出窗口之前,请致电[table reloadData]。这将迫使表格计算尺寸。然后使用[table contentSize]

另请注意,您可以自己计算表格的大小。如果你知道数据和行/页眉/页脚的高度,那么它就是一个简单的乘法和放大的问题。此外。

答案 1 :(得分:0)

我在互联网上的某个地方找到了这个代码,我不确定在哪里或者我会给予信任。它工作得很好(这是在UITableViewController子类中)。它将控制器的宽度设置为比表中显示的最宽线宽(在我的情况下为公开指示器留出空间)。

 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;
}
相关问题