xcode 我希望 UIScrollView 内的 UITableView 扩展到 UIScrollView 的底部,并且不会被初始屏幕截断
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43314864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I want a UITableView inside of a UIScrollView to extend to the bottom of the UIScrollView and not get cut off by initial screen
提问by Ethan Zhao
so I have a viewcontroller that I put a scrollview in. Inside the scrollview, I put a bunch of items in the top half, like an image, labels, and buttons. On the bottom half, I put a uitableview. I set all the constraints and everything. I used the following code:
所以我有一个视图控制器,我在其中放置了一个滚动视图。在滚动视图中,我在上半部分放置了一堆项目,例如图像、标签和按钮。在下半部分,我放了一个 uitableview。我设置了所有的约束和一切。我使用了以下代码:
let bounds: CGRect = UIScreen.main.bounds
let w:Int = Int(bounds.size.width)
let h:Int = Int(chaptersTableView.contentSize.height)
let temp: Int = 300 + h
scrollView.contentSize = CGSize(width: w, height: temp)
chaptersTableView.frame = CGRect (x: 0, y: 280.5, width: self.view.frame.size.width, height: CGFloat(44 * sampleModel.objectChapterList.count))
I wanted to set the contentsize of the scrollview, and then set the height of my tableview frame.
我想设置滚动视图的内容大小,然后设置我的表格视图框架的高度。
The tableview is meant to contain many items, so I want the user to be able to scroll down, and the entire contents of the scrollview to scroll down to reveal more of the tableview. However, the tableview always gets cut off and when I scroll down in the scrollview, the table ends. I have to scroll inside of the tableview to access more cells.
tableview 旨在包含许多项目,所以我希望用户能够向下滚动,并且滚动视图的整个内容向下滚动以显示更多的 tableview。但是,表格视图总是被切断,当我在滚动视图中向下滚动时,表格结束。我必须在 tableview 内部滚动才能访问更多单元格。
Even when I replace the "44 * (number of cells in my table)" with a random number like 100, it will at first cut the table off so that is it short, but then when I tap the screen, the table will change to resume being full length to the bottom of the screen (but not to bottom of scrollview).
即使我用 100 之类的随机数替换“44 *(表格中的单元格数)”,它首先会切断表格,因此它很短,但是当我点击屏幕时,表格会改变恢复到屏幕底部的全长(但不是滚动视图的底部)。
I thought about making all the stuff in the top half of the scrollview just another cell of the tableview so that it can be part of the table and I can avoid this hassle, but that would be a lot of work so I wanted to see if I could do this.
我想过将滚动视图上半部分中的所有内容都制作成表格视图的另一个单元格,这样它就可以成为表格的一部分,我可以避免这种麻烦,但这将是很多工作,所以我想看看我可以做到这一点。
If you need pictures or something, I can provide them. If I'm not being clear, make sure to comment and I can explain better.
如果你需要图片什么的,我可以提供。如果我不清楚,请务必发表评论,我可以更好地解释。
回答by Aashish1aug
I suggest you to use tableView only. You can create your top view in the header of tableView. Doing this there will be no need to maintain the content size of scrollView.
我建议你只使用 tableView。您可以在 tableView 的标题中创建顶视图。这样做将不需要维护 scrollView 的内容大小。
回答by The Dreams Wind
When two scrollable views with the same direction are put to each other, it always cause problems. I think in your case would be better to place only UITableView
on the screen: you can whether place UITableView
within another non-scrollable view, that holds you static content, our put your static content as a header for UITableView
. Let me know if you need me to elaborate the idea. Hope, it'll help.
当两个方向相同的可滚动视图相互放置时,总是会引起问题。我认为在您的情况下,最好仅UITableView
放置在屏幕上:您可以放置UITableView
在另一个不可滚动的视图中,该视图包含静态内容,我们将您的静态内容作为UITableView
. 如果您需要我详细说明这个想法,请告诉我。希望,它会有所帮助。
回答by LE24
i think you should do this:
我认为你应该这样做:
first: (see https://forums.developer.apple.com/thread/81895)
第一:(见https://forums.developer.apple.com/thread/81895)
tableView.estimatedRowHeight = 0
tableView.estimatedSectionHeaderHeight = 0
tableView.estimatedSectionFooterHeight = 0
tableView.rowHeight = 70
tableView.estimatedRowHeight = 0
tableView.estimatedSectionHeaderHeight = 0
tableView.estimatedSectionFooterHeight = 0
tableView.rowHeight = 70
then
然后
tableView.frame.size.height = tableView.contentSize.height
tableView.frame.size.height = tableView.contentSize.height
回答by FireMango
I think what you want to do is to add a static table in a UIScrollView
. As far as I know, a UITableView
is a subclass of UIScrollView
, which means you are putting a UIScrollView
into another one. This might cause the scrolling to have weird behaviours.
我认为您想要做的是在UIScrollView
. 据我所知, aUITableView
是 的子类UIScrollView
,这意味着您将 aUIScrollView
放入另一个中。这可能会导致滚动出现奇怪的行为。
If you have already calculated the content size and set the table frame size, maybe you can try disabling the scroll property of the table view using the following code:
如果您已经计算了内容大小并设置了表格框架大小,也许您可以尝试使用以下代码禁用表格视图的滚动属性:
tableView.scrollEnabled = NO;
Tell me how it goes :)
告诉我进展如何:)