我有一台装有UITableView的ipad popover。在填充表格后,它通常只有几个项目(4-5),所以我正在寻找一种方法来将弹出窗口(contentSizeForViewInPopover)调整为实际的表格高度(所有单元格的高度之和)。
所以,我确实有高度,但我不确定在哪里调用contentSizeForViewInPopover,我确实尝试调用它:viewDidAppear和viewWillAppear,但没有成功,因为它似乎是较晚填充的表,而实际高度仅在稍后才可用。
对此有什么想法吗?谢谢!
编辑:我的单元格根据它们承载的内容有不同的高度,我不能用noOfRows * cellHeight预先计算高度。
发布于 2012-02-15 21:19:42
当我的视图显示为与contentSizeForViewInPopover匹配时,以及当我调用reloadData时,我想要更改UITableView,因为我只在删除或添加行或节时调用它。
下面的方法计算正确的高度和宽度并设置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);
}发布于 2013-04-08 00:19:05
此答案由@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};
}发布于 2011-06-11 06:58:30
间接方法:
使用以下命令设置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.更新答案:
如果单元格大小不同,您可能需要执行以下操作之一:
'sizeToFit'
- (float)calculatedHeight { return textLabel.frame.origin.ytextLabel.frame.size.height5; }
有关为变量text.
调整UITableViewCell大小的信息,请参阅
https://stackoverflow.com/questions/6312821
复制相似问题