UIPopoverController 中的动态 UITableView 高度(contentSizeForViewInPopover)?
我有一个包含 UITableView
的 ipad 弹出窗口。填充表格后,它通常只有几个项目(4-5),所以我正在寻找一种方法将弹出框(contentSizeForViewInPopover
)调整为实际的表格高度(其所有单元格的高度总和)。
所以,我确实有高度,但我不确定在哪里调用 contentSizeForViewInPopover
,我确实尝试在以下位置调用它:viewDidAppear
和 viewWillAppear
但没有成功,因为表格似乎稍后会填充,而实际高度只能稍后获得。
对此有什么想法吗?谢谢!
编辑:我的单元格根据其携带的内容具有不同的高度,我无法使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
当我的视图看起来与
UITableView
匹配时,以及当我调用reloadData
时,我想更改contentSizeForViewInPopover
因为我只在删除或添加时调用它行或部分。以下方法计算正确的高度和宽度并设置
contentSizeForViewInPopover
I wanted to change the
contentSizeForViewInPopover
when my view appeared to match theUITableView
and also when I callreloadData
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
此答案由上述评论之一的 @BenLings 提供。这是一个非常干净的解决方案,值得拥有自己的答案:
This answer is courtesy of @BenLings from one of the comments above. It's a really clean solution and worth having its own answer:
间接方法:
使用查找程序某个点的总行数...设置 UITableViewCell 的自定义高度
...使用 numberOfRowsInSection 获取行数。
如果您有多个部分,请将其与行数结果相乘以获得有效行数。
现在
更新答案:
如果单元格大小不同,您可能需要执行以下操作之一:
将文本强制缩小,以便所有单元格具有相同的高度...这可以是使用完成
'sizeToFit'
您必须使用另一个来找到文本的高度功能。类似...
返回textLabel.frame.origin.ytextLabel.frame.size.height5;
}
https://discussions.apple.com/thread/1525150?开始=0&tstart=0
Indirect Approach :
Set your custom height for your UITableViewCell using
Find the total number of rows at a point of your program ... Using the numberOfRowsInSection, get the number of rows.
If you have more than one section, multiple it with the result for the number of rows for the effective number of rows.
Now
Updated Answer:
If the cell sizes vary, you might have to do do one of these:
Force the text to a smaller size so that all cells have equal height... This can be done using
'sizeToFit'
You will have to find the height of the text using another function. Something like...
return textLabel.frame.origin.ytextLabel.frame.size.height5;
}
You can look at THIS tutorial for resizing UITableViewCell for variable text.
https://discussions.apple.com/thread/1525150?start=0&tstart=0
我从包含表的控制器中这样做:
I did it like this from controller containing a table:
无需创建人工钩子,这效果很好(至少在 iOS 7 上):
There's no need to create an artificial hook, this works well (on iOS 7 at least):
这应该可以解决问题
This should do the trick
获取高度的另一种方法是将
UITableViewController
的视图(将alpha
设置为0.0
)添加到视图层次结构中,然后获取内容大小使用表视图的contentSize
属性。这是可能的,因为表视图是滚动视图的子类。获得内容大小后,设置
contentSizeForViewInPopover
的值,然后将其推送到弹出窗口上。An alternate way to get the height is to add the
UITableViewController
's view with analpha
set to0.0
to the view hierarchy and then get the content size using thecontentSize
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.