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

发布于 2024-11-15 02:04:23 字数 426 浏览 7 评论 0原文

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

所以,我确实有高度,但我不确定在哪里调用 contentSizeForViewInPopover,我确实尝试在以下位置调用它:viewDidAppearviewWillAppear但没有成功,因为表格似乎稍后会填充,而实际高度只能稍后获得。

对此有什么想法吗?谢谢!

编辑:我的单元格根据其携带的内容具有不同的高度,我无法使用 noOfRows * cellHeight 预先计算高度。

I have an ipad popover that contains an UITableView. After the table is populated, it usually has a few items in it only (4-5), so I'm looking for a way to resize the popover (contentSizeForViewInPopover) to the actual table height (the summed height of all its cells).

So, I do have the height but I'm not sure where to call contentSizeForViewInPopover, I did try to call it in: viewDidAppear and viewWillAppear but with no success since it seems that the table gets populated later and the actual height is only available later.

Any thoughts on this? Thanks!

EDIT: My cells have different heights based on the content they carry, I can't pre-calculate the height with noOfRows * cellHeight.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

灵芸 2024-11-22 02:04:23

当我的视图看起来与 UITableView 匹配时,以及当我调用 reloadData 时,我想更改 contentSizeForViewInPopover 因为我只在删除或添加时调用它行或部分。

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

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);
}
回忆躺在深渊里 2024-11-22 02:04:23

此答案由上述评论之一的 @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};
}

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};
}
日暮斜阳 2024-11-22 02:04:23

间接方法:

使用查找程序某个点的总行数...设置 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. 来完成

    您必须使用另一个来找到文本的高度功能。类似...

    • (float)计算高度{
      返回textLabel.frame.origin.ytextLabel.frame.size.height5;
      }
  3. 您可以查看这个 调整 UITableViewCell 大小以适应可变文本的教程。

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

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

明天过后 2024-11-22 02:04:23

我从包含表的控制器中这样做:

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

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;
}
小情绪 2024-11-22 02:04:23

无需创建人工钩子,这效果很好(至少在 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;
}

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;
}
绮筵 2024-11-22 02:04:23

这应该可以解决问题

override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()

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

This should do the trick

override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()

        self.preferredContentSize = CGSizeMake(0, self.tableView.contentSize.height)
 }
虫児飞 2024-11-22 02:04:23

获取高度的另一种方法是将 UITableViewController 的视图(将 alpha 设置为 0.0)添加到视图层次结构中,然后获取内容大小使用表视图的 contentSize 属性。这是可能的,因为表视图是滚动视图的子类。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文