9

我有一个包含UITableView的ipad popover。表填充完后,它通常只包含一些项目(4-5),所以我正在寻找一种方法将弹出窗口(contentSizeForViewInPopover)调整为实际表格高度(所有单元格的总计高度) 。UIPopoverController(contentSizeForViewInPopover)中的动态UITableView高度?

所以,我有高度,但我不知道在哪里打电话contentSizeForViewInPopover,我曾尝试调用它:viewDidAppearviewWillAppear,但没有成功,因为它似乎是表被填充后,实际高度仅在以后可用。

对此有何看法?谢谢!

编辑:我的细胞根据它们携带的内容有不同的高度,我不能用noOfRows * cellHeight预先计算高度。

回答

1

间接的方法:使用

设置你的UITableViewCell的自定义高度

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

找到行的程序点总数...使用numberOfRowsInSection,得到数的行。

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

return [self.yourArray count]; 

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

如果您有多个节,请将其与有效行数的行数结果相乘。

现在

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

更新答:

如果单元格大小各不相同,你可能需要做执行下列操作之一:

  1. 强制文本到一个较小的使所有细胞具有相同的高度...这可以使用

    'sizeToFit'

  2. 您将不得不使用另一个函数来查找文本的高度。喜欢的东西...

    • (浮动)calculatedHeight { 回报textLabel.frame.origin.ytextLabel.frame.size.height5; }
  3. 你可以看一下THIS教程调整UITableViewCell的可变文本。

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

+0

不幸的是,我的单元格根据它们携带的内容而具有不同的高度,我无法用'noOfRows * cellHeight'预先计算高度。 – 2011-06-10 23:05:39

+0

检查更新的答案。 – Legolas 2011-06-10 23:15:26

0

的另一种方法来获得高度是与alpha集添加UITableViewController的视图0.0到视图层级结构,然后使用的所述contentSize属性获取内容大小表视图。这是可能的,因为表格视图是滚动视图的子类。

获得内容大小后,将值设置为contentSizeForViewInPopover,然后将其推送到弹出窗口。

+0

好吧,这很有道理,但我应该在哪里调用'contentSize'?它必须在表填充后​​调用(在'tableView:cellForRowAtIndexPath:'之后又名') – 2011-06-10 23:29:31

+0

在将不可见表视图添加到视图层次结构后获取它。它应该是'addSubview:',接着是'tableView.contentSize'。 – 2011-06-10 23:31:00

18

我想更改contentSizeForViewInPopover,当我的视图与UITableView相匹配时,以及我拨打reloadData时,我只在删除或添加行或部分时调用此选项。

下面的方法计算正确的高度和宽度,并设置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); 
} 
+4

Squeeeeeeeeeee! – 2012-02-15 14:03:35

+5

您可以通过执行'CGRectGetMaxY([self.tableView rectForSection:[self.tableView numberOfSections] - 1])简化计算'' - 您只需要知道最后一部分的底部边缘 – 2012-07-06 13:38:53

+0

感谢Ben Lings,那确实很多简单。 – k107 2013-01-19 01:08:56

11

这个答案是从上述评论中的一个的@BenLings礼貌。这是一个非常干净的解决方案和值得拥有它自己的答案:

- (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}; 
} 
+1

这对我很好,我还必须包含表头或页脚的高度(如果存在)。 – tapi 2013-09-05 16:07:14

+0

我应该在哪里调用这个方法? – Isuru 2015-02-04 15:14:47

1

我没有像这样从控制器包含一个表:

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

有没有必要建立一个人工钩,这个效果很好(在iOS 7至少):

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

这应该做的伎俩

override func viewWillLayoutSubviews() { 
     super.viewWillLayoutSubviews() 

     self.preferredContentSize = CGSizeMake(0, self.tableView.contentSize.height) 
} 
相关问题