简体   繁体   English

iOS Tableview在scrollview的高度

[英]IOS Tableview in a scrollview heights

I have a UITableView nested within a UIScrollView and am finding it hard to calculate the height of either. 我有一个嵌套在UIScrollViewUITableView并且发现很难计算两者的高度。

The content of both is dynamic so I'd like the tableview to expand to fit the content of it, and in turn the scrollview to expand to fit the content of the tableview. 两者的内容都是动态的,因此我希望将tableview扩展为适合其内容,然后将scrollview扩展为适合tableview的内容。

I have this for the scrollview: 我有这个滚动视图:

CGFloat scrollViewHeight = 0.0f;
for (UIView* view in contentView.subviews)
    {
        scrollViewHeight += view.frame.size.height;
    }

But the resultant height is too long, too much scrolling. 但是结果高度太长,滚动太多。

The tableview I'm finding harder to get to dynamically resize, if I leave it without a defined size it is too small. 我发现很难动态调整表视图的大小,如果我将其保留为未定义的大小,则它太小了。 So I added this: 所以我添加了这个:

self.tableView.contentSize = CGSizeMake(320.0f, [self.tableView contentSize].height);

And that's too big. 那太大了。

I think the size of the tableview is dictated by the number and height of the rows within it but can't find a clear answer here or on the net. 我认为tableview的大小取决于其中的行的数量和高度,但在这里或网上找不到明确的答案。

Any ideas? 有任何想法吗?

Your UITableViewDelegate should implement: 您的UITableViewDelegate应该实现:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

This will set the correct size for your cell. 这将为您的单元格设置正确的大小。

I use the following code to set the height of UIScrollView : 我使用以下代码设置UIScrollView的高度:

for (UIView* view in scrollview_.subviews)
{
    if (!view.hidden)
    {
        CGFloat y = view.frame.origin.y;
        CGFloat h = view.frame.size.height;
        if (y + h > scrollViewHeight)
        {
            scrollViewHeight = h + y;
        }
    }
}

[scrollview_ setContentSize:(CGSizeMake(320, scrollViewHeight))];

To get the height of UITableView , maybe you can calculate like rowheight*rowcount 要获取UITableView的高度,也许您可​​以像rowheight * rowcount那样进行计算

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM