简体   繁体   English

如何使 UITableView 适合内容的大小

[英]How to make UITableView fit to content's size

I have a UITableView (let's call it tbl_A) inside a table view cell whose height is dynamic and set by UITableViewAutomaticDimension .我在表格视图单元格中有一个UITableView (我们称之为 tbl_A),它的高度是动态的并由UITableViewAutomaticDimension设置。 tbl_A's cell height is also dynamic, and I know tbl_A's data won't be too much. tbl_A 的单元格高度也是动态的,我知道 tbl_A 的数据不会太多。 So I want tbl_A to be scroll disabled and its frame.size equals its content size .所以我希望 tbl_A 被禁用滚动并且它的frame.size 等于它的 content size I tried to set tbl_A's frame.size.height = tbl_A.contentSize.height, unfortunately, the height is wrong.我试着设置tbl_A的frame.size.height = tbl_A.contentSize.height,不幸的是,高度不对。

By the way, I don't have to use UITableView to accomplish this task.顺便说一句,我不必使用UITableView来完成这个任务。 I just want to a way to display all data.我只是想要一种显示所有数据的方法。 Any suggestions?有什么建议?

This is the effect I want to achieve:这是我想要达到的效果: 在此处输入图片说明

Simple ;)简单的 ;)

override var intrinsicContentSize: CGSize {
    return contentSize
}

Based on @Emad's answer but with the important addition to invalidate the intrinsic content size if the contentSize changes.基于@Emad 的回答,但如果 contentSize 发生变化,那么重要的补充是使内在内容大小无效。

class ContentWrappingTableView: UITableView {

  override var intrinsicContentSize: CGSize {
    return self.contentSize
  }

  override var contentSize: CGSize {
    didSet {
        self.invalidateIntrinsicContentSize()
    }
  }
}

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 内容后的内容大小配置高度约束,以确保在调整大小之前加载内容。

  1. For your case you can also use scrollView.对于您的情况,您还可以使用 scrollView。
  2. In that scrollView while adding each subcell keep track of each subcell by adding each subcell's height ( take variable as referenceHeight )在该 scrollView 中添加每个子单元格时通过添加每个子单元格的高度来跟踪每个子单元格(将变量作为 referenceHeight )
  3. finally set the scrollview height by calculated height(referenceHeight)最后通过计算高度(referenceHeight)设置滚动视图高度

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];

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()

在您的情况下 tableview 是最好的解决方案,您可以使用不同的 uitableviewcells 和部分,并且可以在 cellForRowAtIndexPath 中加载它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM