Nested variable height UITableView in UITableViewCell + UITableViewAutomaticDimension

I have UITableViewone in which cells contain ImageViewand nested UITableView. A nested table can contain a variable number of cells and, therefore, has a variable height (and it does not scroll).

I am trying to set the height of the parent cell to be equal to the height of the image and the nested table view using UITableViewAutomaticDimension, but it only extends to the image, probably because the table view starts at a height of 0 and fills after it is created in the parent cell.

How to make cells of the parent table the same height than their contents? I have already set limits between the presentation of the contents of the cell and the graphics / nested table.

+4
source share
2 answers

I had a similar problem when trying to implement nested tables. I found two solutions to this problem.

  • Calculating the height of the innerTableView and setting the rowHeight of the external tableView to it by doing the following in it: cellForRowAtIndexPath

rowHeight = cell.innerTableView.contentSize.height

return rowHeight in tableView: evaluatedHeightForRowAtIndexPath and tableView: heightForRowAtIndexPath

, View cellHeight .

  1. . http://vuta.info/dynamic-uitableviews-height-with-auto-layout-in-swift/
0

. .

UITableViewAutomaticDimension tableView.rowHeight.

.

innerTableView.isScrollEnabled = false

, .

, :

  • = 100
  • = 30

, View (tableView: heightForRowAt), :

// constant
let imgHeight = 100
let innerCellHeight = 30

// inner cell count
let dataSource = dataSources[indexPath.row]
let innerCellsCount = dataSource.items.count


return CGFloat( imgHeight + ( innerCellsCount * innerCellHeight ) )

tableView (tableView: evaluationHeightForRowAt), , , , .

-1

Source: https://habr.com/ru/post/1629089/


All Articles