UIScrollView中UITableView的内容大小问题

7

我实际上是使用界面构建器创建了一个包含UITableViewUIScrollView。我通过以下方式设置了我的UIScrollView的内容大小:

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

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

我在界面生成器中创建了一个 IBOutlet UITableView 并将其设置。

当视图加载时,它会显示我的 UIScrollView 的正确内容大小。但是它不会显示我设置在界面生成器中的 UITableView 的正确内容大小。此外,当我更改我的 UIScrollView 的内容大小时,它会像两个内容大小相关联一样更改我的 UITableView 的内容大小。

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

4个回答

13

UITableView是一个UIScrollView。嵌入UITableViewUIScrollView里面的情况非常少,除非你真的需要这样做。

假设你确实需要这样做,那么你需要在某个地方执行以下操作。可能是在你的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;

仍然不起作用...实际上,当我NSLog tableView.frame时,它显示为(null)。我不明白... - kschaeffler
实际上,我的文件所有者是UIScrollView和UITableView的代理,它是一个scrollView。这会有问题吗? - kschaeffler
我应该更加明确。tableViewscrollView应该是您与这些项目关联的IBOutlets。 - DBD
是的,我的IBOutlets tableView和scrollView与我的文件所有者相关联。 - kschaeffler
这是因为您试图将结构体的内容打印为字符串。您可以打印对象,因为它只是在对象上调用“description”方法,但结构体没有这样的能力。 - DBD
显示剩余4条评论

1

示例代码,用于Swift动态管理TableView高度和ScrollView,如下:

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
    }

参考资料:


0

如果你仍然遇到问题,可以尝试不使用scrollView。

在尝试了一段时间DBD的示例后,我发现frame和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中或代码中的自动调整大小掩码,但这个技巧是我发现的唯一解决所有不同变量的情况。


0

多次调用以下方法。每次调用时都会有更多的单元格。

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

scrollView.contentSize = tableView.contentSize;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接