简体   繁体   中英

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 . 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.

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?

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.

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.:

    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.

  1. For your case you can also use scrollView.
  2. In that scrollView while adding each subcell keep track of each subcell by adding each subcell's height ( take variable as referenceHeight )
  3. finally set the scrollview height by calculated height(referenceHeight)

Below is method to find tableView's content size.

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

var heightC: NSLayoutConstraint!

2) Disable scroll and add height constraint:

tableView.scrollEnabled = false
heightC = tableView.autoSetDimension(.Height, toSize: 20) // using PureLayout library

3) override viewDidLayoutSubviews of your controller:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    heightC.constant = tableView.contentSize.height
}

and set height constraint "constant" attribute to tableview content height.

4) on data refresh (for example when item count changes from 0 to more) just force main controller view to layout:

view.setNeedsLayout()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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