首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Dynamic UITableView height in UIPopoverController (contentSizeForViewInPopover)?

Dynamic UITableView height in UIPopoverController (contentSizeForViewInPopover)?
EN

Stack Overflow用户
提问于 2011-06-11 06:50:42
回答 7查看 13.8K关注 0票数 9

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

所以,我确实有高度,但我不确定在哪里调用contentSizeForViewInPopover,我确实尝试调用它:viewDidAppearviewWillAppear,但没有成功,因为它似乎是较晚填充的表,而实际高度仅在稍后才可用。

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

编辑:我的单元格根据它们承载的内容有不同的高度,我不能用noOfRows * cellHeight预先计算高度。

EN

回答 7

Stack Overflow用户

发布于 2012-02-15 21:19:42

当我的视图显示为与contentSizeForViewInPopover匹配时,以及当我调用reloadData时,我想要更改UITableView,因为我只在删除或添加行或节时调用它。

下面的方法计算正确的高度和宽度并设置contentSizeForViewInPopover

代码语言:javascript
复制
-(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);
}
票数 18
EN

Stack Overflow用户

发布于 2013-04-08 00:19:05

此答案由@BenLings提供,来自上面的一条评论。这是一个非常干净的解决方案,值得有自己的答案:

代码语言:javascript
复制
- (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};
}
票数 11
EN

Stack Overflow用户

发布于 2011-06-11 06:58:30

间接方法:

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

代码语言:javascript
复制
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [indexPath row] * 40;
}

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

代码语言:javascript
复制
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

return [self.yourArray count];

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

如果有多个区段,则将其与有效行数的行数结果相乘。

现在

代码语言:javascript
复制
UITableView Actual Height = Height of UITableViewCell * Total number of rows.

更新答案:

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

  1. 强制文本为较小的大小,以便所有单元格具有相同的高度...这可以使用以下命令来完成

'sizeToFit'

  • You必须使用另一个函数来确定文本的高度。就像...

代码语言:javascript
复制
- (float)calculatedHeight { return textLabel.frame.origin.ytextLabel.frame.size.height5; }

有关为变量text.

调整UITableViewCell大小的信息,请参阅

  1. THIS 教程
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6312821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档