How to make UITableView suitable for content size

I have a UITableView (name it tbl_A) inside a table view cell whose height is dynamic and set to UITableViewAutomaticDimension . The height of the tbl_A cell is also dynamic, and I know that the tbl_A data will not be too large. So I want tbl_A to be scroll disabled , and its frame.size equal to its content size . I tried setting tbl_A frame.size.height = tbl_A.contentSize.height, unfortunately the height is wrong.

By the way, I do not need to use a UITableView to complete this task. I just want to show all the data. Any suggestions?

This is the effect I want to achieve: enter image description here

+7
ios uitableview swift
source share
6 answers

Plain;)

 override var intrinsicContentSize: CGSize { return contentSize } 
+5
source share

While scrolling is locked, you can use the following to dynamically map the table layout:

  let size = actionsViewController.view.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize) actionsViewController.view.frame.size = size actionsViewController.preferredContentSize = size 

Below is a way to automatically calculate the height of the layout for you and provide you with a size based on the constraints you are passing. 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; 

Earlier, I used the above to expand dynamically resizing cells with dynamic subzones inside them. This sets up a height limit based on the size of the content AFTER the contents of the tableView are reloaded to allow the content to load before adjusting the size.

+2
source share
  • In your case, you can also use scrollView.
  • In this scrollView, when adding each sub-section, track each sub-section, adding each sub-element height (accept the variable as referenceHeight)
  • finally set the scroll height to the calculated height (referenceHeight)
0
source share

The following is a method for determining the size of the contents of a table.

 - (CGFloat)tableViewHeightOfTable:(UITableView *)table { [table layoutIfNeeded]; return [table contentSize].height; } 

Make sure you call this method after reloading the table, as shown below.

 [YourTable reloadData]; YourTable.frame.size.height = [self tableViewHeightOfTable:YourTable]; 
0
source share

1) Add a restriction link to your class:

 var heightC: NSLayoutConstraint! 

2) Disable scrolling and add height limit:

 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 the "const" attribute of the height limit for the height of the table contents.

4) when updating data (for example, when the number of elements changes from 0 to more), it simply forcibly displays the representation of the main controller in the layout:

 view.setNeedsLayout() 
0
source share

In your case, tableview is the best solution, you can use various uitableviewcells and sections and you can load it into cellForRowAtIndexPath.

-2
source share

All Articles