UIPopoverController中的动态UITableView高度(contentSizeForViewInPopover)?

Val*_*adu 9 cocoa-touch objective-c uitableview ipad uipopovercontroller

我有一个包含的ipad popover UITableView.填充表后,它通常只有几个项目(4-5),所以我正在寻找一种方法来将popover(contentSizeForViewInPopover)的大小调整为实际的表高度(所有单元格的总高度).

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

有什么想法吗?谢谢!

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

She*_*eep 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);
}
Run Code Online (Sandbox Code Playgroud)

  • 你可以通过`CGRectGetMaxY([self.tableView rectForSection:[self.tableView numberOfSections] - 1])来简化计算 - 你只需要知道最后一节的下边缘 (5认同)
  • Squeeeeeeeeeee! (4认同)

mem*_*ons 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};
}
Run Code Online (Sandbox Code Playgroud)


Leg*_*las 1

间接方法:

使用以下命令设置 UITableViewCell 的自定义高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [indexPath row] * 40;
}
Run Code Online (Sandbox Code Playgroud)

查找程序某个点的总行数...使用 numberOfRowsInSection 获取行数。

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

return [self.yourArray count];

//yourArray is the array with which you are populating the tableView
}
Run Code Online (Sandbox Code Playgroud)

如果您有多个部分,请将其与行数结果相乘以获得有效行数。

现在

UITableView Actual Height = Height of UITableViewCell * Total number of rows.
Run Code Online (Sandbox Code Playgroud)

更新答案:

如果单元格大小不同,您可能必须执行以下操作之一:

  1. 强制文本较小的尺寸,以便所有单元格具有相同的高度...这可以使用

    '尺寸适合'

  2. 您必须使用另一个函数找到文本的高度。就像是...

    • (float)calculatedHeight { return textLabel.frame.origin.ytextLabel.frame.size.height5; }
  3. 您可以查看教程,了解如何调整可变文本的 UITableViewCell 大小。

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