UIScrollView 中 UITableView 的内容大小问题

发布于 2024-12-26 05:59:08 字数 581 浏览 3 评论 0原文

我实际上使用界面生成器创建了一个 UIScrollView ,其中包含一个 UITableView 。我设置了 UIScrollView 的内容大小:

scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);

并将文件的所有者视图设置为 UIScrollView

我创建了一个 IBOutlet UITableView< /code> 我在界面生成器中设置了它。

当视图加载时。它显示我的 UIScrollView 的正确内容大小。但它没有显示我的 UITableView 的正确内容大小。此外,当我更改 UIScrollView 的内容大小时,它也会更改 UITableView 的内容大小,就好像两个内容大小相关一样。

有人知道如何保持在界面生成器中设置的表格视图的内容大小吗?

I actually create a UIScrollView with a UITableView inside it with interface builder. I set the content size of my UIScrollView with:

scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);

and I set my file's owner view to the UIScrollView

I created an IBOutlet UITableView and I set it in the interface builder.

When the view is loaded. it displays the correct content size for my UIScrollView. But it doesn't display the correct content size for my UITableView. Besides when I change the content size of my UIScrollView, it changes the content size of my UITableView as if both content size were related.

Anyone knows how can I keep the content size of my table view that I set in the interface builder?

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

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

发布评论

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

评论(4

清秋悲枫 2025-01-02 05:59:08

UITableView 是一个UIScrollView。在极少数情况下,将 UITableView 嵌入到 UIScrollView 中会很有用。

假设您确实想要这个,您需要在某处执行以下操作。可能在您的 UIViewController 中,例如 viewDidAppear: 等方法中,或者在填充表视图后的某个地方。

// Set the size of your table to be the size of it's contents 
// (since the outer scroll view will handle the scrolling).
CGRect tableFrame = tableView.frame;
tableFrame.size.height = tableView.contentSize.height;
tableFrame.size.width = tableView.contentSize.width; // if you would allow horiz scrolling
tableView.frame = tableFrame;

// Set the content size of your scroll view to be the content size of your 
// table view + whatever else you have in the scroll view.
// For the purposes of this example, I'm assuming the table view is in there alone.
scrollView.contentSize = tableView.contentSize;

A UITableView is a UIScrollView. There are very few times when it would be useful to embed a UITableView inside a UIScrollView.

Assuming you really do want this, you'll need to do the following somewhere. Probably in your UIViewController in methods like viewDidAppear: or somewhere you after you populate the table view.

// Set the size of your table to be the size of it's contents 
// (since the outer scroll view will handle the scrolling).
CGRect tableFrame = tableView.frame;
tableFrame.size.height = tableView.contentSize.height;
tableFrame.size.width = tableView.contentSize.width; // if you would allow horiz scrolling
tableView.frame = tableFrame;

// Set the content size of your scroll view to be the content size of your 
// table view + whatever else you have in the scroll view.
// For the purposes of this example, I'm assuming the table view is in there alone.
scrollView.contentSize = tableView.contentSize;
再浓的妆也掩不了殇 2025-01-02 05:59:08

动态管理 TableView 高度和 ScrollView 的示例,适用于 Swift:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // Custom Cell
        let cell:TblPortCell = self.tableList.dequeueReusableCellWithIdentifier("cell") as! TblPortCell

        //tableList is the OutLet for the table
        tableList.sizeToFit()
        // Set the size of the table to be the size of it's contents
        // (since the outer scroll view will handle the scrolling).
        let height = tableList.frame.size.height
        let pos = tableList.frame.origin.y
        let sizeOfContent = height + pos + 130
        //scrollView is the Outlet for the Scroll view
        scrollView.contentSize.height = sizeOfContent
        scrollView.sizeToFit()

        cell.selectionStyle = UITableViewCellSelectionStyle.None
        return cell
    }

参考:

Example to dynamically manage the TableView Height along with ScrollView, works in Swift:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // Custom Cell
        let cell:TblPortCell = self.tableList.dequeueReusableCellWithIdentifier("cell") as! TblPortCell

        //tableList is the OutLet for the table
        tableList.sizeToFit()
        // Set the size of the table to be the size of it's contents
        // (since the outer scroll view will handle the scrolling).
        let height = tableList.frame.size.height
        let pos = tableList.frame.origin.y
        let sizeOfContent = height + pos + 130
        //scrollView is the Outlet for the Scroll view
        scrollView.contentSize.height = sizeOfContent
        scrollView.sizeToFit()

        cell.selectionStyle = UITableViewCellSelectionStyle.None
        return cell
    }

References to:

我偏爱纯白色 2025-01-02 05:59:08

如果您仍然遇到问题,请在不需要滚动视图的情况下尝试此操作。

在玩了一段时间 DBD 的示例后,我发现框架和 contentSize 似乎无法设置在一起,如下所示:

self.tableView.contentSize = CGSizeMake(320, scrollHeight);
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,44,self.tableView.contentSize.width,self.tableView.contentSize.height);

尝试设置框架的最大高度,但保持滚动所有框架的能力如果存在大量细胞则含量。这个黑客似乎运作良好,并且如果需要的话可以根据更多条件进行修改:

int cellCount = [YOURARRAY count];
CGFloat scrollHeight = cellCount*44+44;//for a cell with height of 44 and adding 44 if you have a toolbar at the bottom

self.tableView.contentSize = CGSizeMake(320, scrollHeight);//you can change the width and or let the .xib control the autoresizing

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && cellCount < 14)
{
    //here's were you can add more conditions if you're in landscape or portrait, but this handles 14 cells with a heightForHeaderInSection of 46 in landscape
    self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,44,self.tableView.contentSize.width,scrollHeight);
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && cellCount < 7)
{
     //same as above but it's for the iPhone in portrait
     self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,44,self.tableView.contentSize.width,scrollHeight);
}

这肯定有效。您可能需要调整 .xib 或代码中的 autoresizingMask,但此 hack 是我发现的唯一解决方案,可以处理我的案例中的所有不同变量。

If you're still having problems, try this without the need for a scrollView.

After playing around with DBD's example for awhile, I found that the frame and contentSize don't seem to be able to be set together like:

self.tableView.contentSize = CGSizeMake(320, scrollHeight);
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,44,self.tableView.contentSize.width,self.tableView.contentSize.height);

The attempt was to set a max height of the frame but keep the ability to scroll though all of the content if there were a large number of cells. This hack seems to work well and can be modified with more conditions if needed:

int cellCount = [YOURARRAY count];
CGFloat scrollHeight = cellCount*44+44;//for a cell with height of 44 and adding 44 if you have a toolbar at the bottom

self.tableView.contentSize = CGSizeMake(320, scrollHeight);//you can change the width and or let the .xib control the autoresizing

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && cellCount < 14)
{
    //here's were you can add more conditions if you're in landscape or portrait, but this handles 14 cells with a heightForHeaderInSection of 46 in landscape
    self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,44,self.tableView.contentSize.width,scrollHeight);
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && cellCount < 7)
{
     //same as above but it's for the iPhone in portrait
     self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,44,self.tableView.contentSize.width,scrollHeight);
}

This works for sure. You might need to adjust the autoresizingMask inside your .xib or in your code, but this hack is the only solution I found that takes care of all the different variables in my case.

写给空气的情书 2025-01-02 05:59:08

多次调用下面的方法。每次你打电话都会有更多的细胞。

CGRect tableFrame = tableView.frame;
tableFrame.size.height = tableView.contentSize.height;
tableView.frame = tableFrame;

scrollView.contentSize = tableView.contentSize;

Call below method multiple times. Each time you call there will be more cells.

CGRect tableFrame = tableView.frame;
tableFrame.size.height = tableView.contentSize.height;
tableView.frame = tableFrame;

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