ios 使 UITableView 不可滚动并调整高度以容纳所有单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24090104/
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
Make UITableView not scrollable and adjust height to accommodate all cells
提问by Allen
I'm embedding a UITableView
inside of another UIScrollView
. I only want the UIScrollView
to scroll, not the UITableView
, so I want to disable the scrolling in UITableView
, as well as expand the contentHeight
for the UITableView
to accommodate all dynamic at once.
我正在嵌入UITableView
另一个UIScrollView
. 我只希望UIScrollView
到滚动,而不是UITableView
,所以我想禁用滚动UITableView
,以及扩大contentHeight
为UITableView
容纳所有动态的一次。
How would I go about doing this?
我该怎么做呢?
回答by Learner
Set tableView scrolling property to false.
将 tableView 滚动属性设置为 false。
First Approach:
第一种方法:
Create a subclass for tableView and override intrinsicContentSize.
class MyOwnTableView: UITableView { override var intrinsicContentSize: CGSize { self.layoutIfNeeded() return self.contentSize } override var contentSize: CGSize { didSet{ self.invalidateIntrinsicContentSize() } } override func reloadData() { super.reloadData() self.invalidateIntrinsicContentSize() } }
In Interface builder change the class of your tableView to MyOwnTableView (subclass UITableView).
Set automatic row height for the table view.
tableView.estimatedRowHeight = 60.0; tableView.rowHeight = UITableViewAutomaticDimension;
为 tableView 创建一个子类并覆盖 internalContentSize。
class MyOwnTableView: UITableView { override var intrinsicContentSize: CGSize { self.layoutIfNeeded() return self.contentSize } override var contentSize: CGSize { didSet{ self.invalidateIntrinsicContentSize() } } override func reloadData() { super.reloadData() self.invalidateIntrinsicContentSize() } }
在界面生成器中,将 tableView 的类更改为 MyOwnTableView(子类 UITableView)。
为表格视图设置自动行高。
tableView.estimatedRowHeight = 60.0; tableView.rowHeight = UITableViewAutomaticDimension;
Second Approach:1. Create a height constraint with any value for tableView and connect an IBOutlet that sets the tableView height.
第二种方法:1. 为tableView 创建一个具有任意值的高度约束,并连接一个设置tableView 高度的IBOutlet。
Set the height constraint's constant to tableView.contentSize.height after reloading the data.
self.tableViewHeight.constant = self.tableView.contentSize.height
重新加载数据后,将高度约束的常量设置为 tableView.contentSize.height。
self.tableViewHeight.constant = self.tableView.contentSize.height
回答by Ahsan
You need to set the UITableView
scroll to false
,
您需要将UITableView
滚动设置为false
,
tableview.scrollEnabled = false;
tableview.frame = CGRectMake(tableview.frame.origin.x, tableview.frame.origin.y, tableview.frame.size.width, tableview.frame.size.height+(resultArray.count*HEIGHT_OF_CELL));
And then you can add the UITableView
on UIScrollView
by setting it's content size as UITableView
height.
然后你就可以添加UITableView
上UIScrollView
通过设置它的内容大小UITableView
的高度。
回答by Huy Le
Set tableView size equal with contentSize.
将 tableView 大小设置为与 contentSize 相等。
override func viewDidLayoutSubviews() {
tableView.frame.size = tableView.contentSize
}
回答by preetam
Basically, tableview's content size is all you need.
基本上,tableview 的内容大小就是你所需要的。
Set tableview scrolling to
false
because we don't want scolling behaviour.Get the reference of tableview's height constraint.
set
tableViewHeightConstraint.constant = tableView.contentSize.height
将 tableview 滚动设置为
false
因为我们不想要滚动行为。获取tableview高度约束的参考。
放
tableViewHeightConstraint.constant = tableView.contentSize.height
Make sure, tableview's top, bottom, leading and trailing constraint and are in-place.
确保 tableview 的顶部、底部、前导和尾随约束就位。
回答by Duyen-Hoa
- Make your tableview frame cover all lines possible -> no scroll for table view
- Set contentSize of your scrollview = size of your tableViews.
- 使您的表格视图框架覆盖所有可能的行-> 表格视图不滚动
- 设置滚动视图的 contentSize = tableViews 的大小。
回答by BlaShadow
- First of all you need is to set up the
UITableView
Scrolling toNO
Second, create a
height constraint
for the table view and after you call[UITableView reloadData]
change yourheight constraint
tableViewHeightConstraint.constant = numberOfRows * heightOfEachRow;
- 首先,您需要设置
UITableView
滚动到NO
其次,
height constraint
为表视图创建一个,并在调用后[UITableView reloadData]
更改您的height constraint
tableViewHeightConstraint.constant = numberOfRows * heightOfEachRow;
回答by J2theC
UITableView is a scroll view. While you are not supposed to nest UIScrollviews, you can disable scrolling of the the table view.
UITableView 是一个滚动视图。虽然您不应该嵌套 UIScrollviews,但您可以禁用表格视图的滚动。
回答by Lytic
While this is very simple to accomplish with a fixed-content tableview as a subview in a scrollview, what is it you are exactly trying to accomplish? Dynamic cells with fixed content above and below? Permanently displayed content with a sliding section in the center?
虽然使用固定内容的 tableview 作为滚动视图中的子视图来实现这一点非常简单,但您究竟想要完成什么?上下固定内容的动态单元格?永久显示的内容,中间有一个滑动部分?
Perhaps a cleaner design would be using a single UITableView with a Table Header and Footer, or a Section Header an Footer leveraging the automatic behavior provided by UITableViewStyleGrouped vs UITableViewStylePlain
也许更简洁的设计是使用带有表格页眉和页脚的单个 UITableView,或者利用 UITableViewStyleGrouped 与 UITableViewStylePlain 提供的自动行为的节页眉页脚
That said, other answers seem to form a decent solution:
也就是说,其他答案似乎形成了一个不错的解决方案:
Set scroll enabled false on table view
Set tableview content size to sum of all cell sizes
Set tableview frame to content size
Adjust scroll view frame to account for dynamic table size if necessary
Add table view to scroll view amidst other content
Another option for manipulating scroll behavior would be checking the tableview or scrollview in the delegate scrollView shouldStartScrolling or similar methods, as this would allow more flexibility over the UI controls
操纵滚动行为的另一个选项是检查委托 scrollView shouldStartScrolling 或类似方法中的 tableview 或 scrollview,因为这将允许对 UI 控件具有更大的灵活性