I have a UITableView
(let's call it tbl_A) inside a table view cell whose height is dynamic and set by UITableViewAutomaticDimension
. tbl_A's cell height is also dynamic, and I know tbl_A's data won't be too much. So I want tbl_A to be scroll disabled and its frame.size equals its content size. I tried to set tbl_A's frame.size.height = tbl_A.contentSize.height, unfortunately, the height is wrong.
我有一个UITableView(让我们称之为tbl_A)在一个表格视图单元格中,其高度是动态的,由UITableViewAutomaticDimension设置。 tbl_A的单元格高度也是动态的,我知道tbl_A的数据不会太多。所以我希望tbl_A被滚动禁用,其frame.size等于其内容大小。我试图设置tbl_A的frame.size.height = tbl_A.contentSize.height,遗憾的是,高度错误。
By the way, I don't have to use UITableView
to accomplish this task. I just want to a way to display all data. Any suggestions?
顺便说一下,我不必使用UITableView来完成这项任务。我只是想要一种显示所有数据的方法。有什么建议么?
This is the effect I want to achieve:
这是我想要实现的效果:
6 个解决方案
#1
7
Simple ;)
简单;)
override var intrinsicContentSize: CGSize {
return contentSize
}
#2
2
As long as scrolling is locked, you can use the following to match the tableView's layout dynamically.:
只要滚动被锁定,您就可以使用以下内容动态匹配tableView的布局:
let size = actionsViewController.view.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
actionsViewController.view.frame.size = size
actionsViewController.preferredContentSize = size
The following presents a way to have auto layout calculate the height for you and provide you a size based on restrictions you pass in. Another way could be the following:
下面介绍了一种自动布局为您计算高度的方法,并根据您传入的限制为您提供大小。另一种方法可能如下:
CGFloat leftViewHeight = 0;
if (self.isExpanded) {
[self.lineItemLineNumberListTableViewController.tableView layoutIfNeeded];
UITableView *tableView = self.lineItemLineNumberListTableViewController.tableView;
CGSize tableViewContentSize = tableView.contentSize;
leftViewHeight = tableViewContentSize.height;
}
self.leftViewHeightConstraint.constant = leftViewHeight;
I had previously used the above to expand dynamically resizing cells with dynamic subviews within them. This configures a height constraint based on the content size AFTER reloading the tableView's content to ensure the content was loaded prior to adjusting the size.
我以前使用上面的内容来动态扩展动态调整大小的单元格。这将根据重新加载tableView内容后的内容大小配置高度约束,以确保在调整大小之前加载内容。
#3
0
- For your case you can also use scrollView.
- 对于您的情况,您也可以使用scrollView。
- In that scrollView while adding each subcell keep track of each subcell by adding each subcell's height ( take variable as referenceHeight )
- 在添加每个子单元的scrollView中,通过添加每个子单元的高度来跟踪每个子单元(将变量作为referenceHeight)
- finally set the scrollview height by calculated height(referenceHeight)
- 最后通过计算高度设置scrollview高度(referenceHeight)
#4
0
Below is method to find tableView's content size.
下面是查找tableView的内容大小的方法。
- (CGFloat)tableViewHeightOfTable:(UITableView *)table
{
[table layoutIfNeeded];
return [table contentSize].height;
}
Make sure that you call this method after reloading your table like below code.
确保在重新加载表后调用此方法,如下面的代码。
[YourTable reloadData];
YourTable.frame.size.height = [self tableViewHeightOfTable:YourTable];
#5
0
1) Add to your class reference to constraint:
1)向您的类添加对约束的引用:
var heightC: NSLayoutConstraint!
2) Disable scroll and add height constraint:
2)禁用滚动并添加高度约束:
tableView.scrollEnabled = false
heightC = tableView.autoSetDimension(.Height, toSize: 20) // using PureLayout library
3) override viewDidLayoutSubviews of your controller:
3)覆盖控制器的viewDidLayoutSubviews:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
heightC.constant = tableView.contentSize.height
}
and set height constraint "constant" attribute to tableview content height.
并将高度约束“常量”属性设置为tableview内容高度。
4) on data refresh (for example when item count changes from 0 to more) just force main controller view to layout:
4)关于数据刷新(例如,当项目计数从0更改为更多时)只需将主控制器视图强制为布局:
view.setNeedsLayout()
#6
-2
In your case tableview is the best solution , you can use different uitableviewcells and sections and can load it in cellForRowAtIndexPath .
在你的情况下tableview是最好的解决方案,你可以使用不同的uitableviewcells和section,并可以在cellForRowAtIndexPath中加载它。
#1
7
Simple ;)
简单;)
override var intrinsicContentSize: CGSize {
return contentSize
}
#2
2
As long as scrolling is locked, you can use the following to match the tableView's layout dynamically.:
只要滚动被锁定,您就可以使用以下内容动态匹配tableView的布局:
let size = actionsViewController.view.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
actionsViewController.view.frame.size = size
actionsViewController.preferredContentSize = size
The following presents a way to have auto layout calculate the height for you and provide you a size based on restrictions you pass in. Another way could be the following:
下面介绍了一种自动布局为您计算高度的方法,并根据您传入的限制为您提供大小。另一种方法可能如下:
CGFloat leftViewHeight = 0;
if (self.isExpanded) {
[self.lineItemLineNumberListTableViewController.tableView layoutIfNeeded];
UITableView *tableView = self.lineItemLineNumberListTableViewController.tableView;
CGSize tableViewContentSize = tableView.contentSize;
leftViewHeight = tableViewContentSize.height;
}
self.leftViewHeightConstraint.constant = leftViewHeight;
I had previously used the above to expand dynamically resizing cells with dynamic subviews within them. This configures a height constraint based on the content size AFTER reloading the tableView's content to ensure the content was loaded prior to adjusting the size.
我以前使用上面的内容来动态扩展动态调整大小的单元格。这将根据重新加载tableView内容后的内容大小配置高度约束,以确保在调整大小之前加载内容。
#3
0
- For your case you can also use scrollView.
- 对于您的情况,您也可以使用scrollView。
- In that scrollView while adding each subcell keep track of each subcell by adding each subcell's height ( take variable as referenceHeight )
- 在添加每个子单元的scrollView中,通过添加每个子单元的高度来跟踪每个子单元(将变量作为referenceHeight)
- finally set the scrollview height by calculated height(referenceHeight)
- 最后通过计算高度设置scrollview高度(referenceHeight)
#4
0
Below is method to find tableView's content size.
下面是查找tableView的内容大小的方法。
- (CGFloat)tableViewHeightOfTable:(UITableView *)table
{
[table layoutIfNeeded];
return [table contentSize].height;
}
Make sure that you call this method after reloading your table like below code.
确保在重新加载表后调用此方法,如下面的代码。
[YourTable reloadData];
YourTable.frame.size.height = [self tableViewHeightOfTable:YourTable];
#5
0
1) Add to your class reference to constraint:
1)向您的类添加对约束的引用:
var heightC: NSLayoutConstraint!
2) Disable scroll and add height constraint:
2)禁用滚动并添加高度约束:
tableView.scrollEnabled = false
heightC = tableView.autoSetDimension(.Height, toSize: 20) // using PureLayout library
3) override viewDidLayoutSubviews of your controller:
3)覆盖控制器的viewDidLayoutSubviews:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
heightC.constant = tableView.contentSize.height
}
and set height constraint "constant" attribute to tableview content height.
并将高度约束“常量”属性设置为tableview内容高度。
4) on data refresh (for example when item count changes from 0 to more) just force main controller view to layout:
4)关于数据刷新(例如,当项目计数从0更改为更多时)只需将主控制器视图强制为布局:
view.setNeedsLayout()
#6
-2
In your case tableview is the best solution , you can use different uitableviewcells and sections and can load it in cellForRowAtIndexPath .
在你的情况下tableview是最好的解决方案,你可以使用不同的uitableviewcells和section,并可以在cellForRowAtIndexPath中加载它。