0

I want to have self sizing cells within a tableview with self sizing cells. But on first initialisation some cells are not correct. After a scroll to the bottom and the cells will display again the cell size is correct. In the screenshot you can see that the top 2 cells have a white space at the bottom and the third one doesn't. They should all look like the third one.

enter image description here

This is the issue in my main project. Screenshot

BasM
  • 25
  • 7
  • why not using sections? – Deryck Lucian May 10 '19 at 10:06
  • @DeryckLucian Okay, maybe my explanation is not fully clear. See my new added screenshot. I want to have that extra depth of the tableview inside the tableview. – BasM May 10 '19 at 10:13
  • You can utilize the UITableView Section instead to add extra depth – Sharkes Monken May 10 '19 at 10:36
  • You can create a `collectionView` inside the `tableViewCell` instead. For the solution you can refer to https://medium.freecodecamp.org/how-to-make-height-collection-views-dynamic-in-your-ios-apps-7d6ca94d2212. In case you want `tableView` inside the `tableViewCell`, you can follow the same approach for that as well. Let me know if you need any help regarding that. – PGDev May 10 '19 at 10:53
  • @PGDev Your example has the same issue that the cell sizes are not correct at initialisation and are correct after you scroll the cell outside the view and scroll it back inside. – BasM May 10 '19 at 11:22

2 Answers2

1

The problem with automatically sizing cells inside automatically sized cells is a bit tricky because you have to understand how it works. UITableView works with estimates most of the time. It usually does not calculate the contentSize precisely because to calculate it precisely, it has to first instantiate every cell, layout it and then calculate its size.

The precise values are calculated only for cells that are displayed (visible in current scroll frame).

The tricky part is that the inner cells (inside your outer cell) are not displayed until the outer cell is displayed therefore the outer cell does not have size calculated correctly. Also note that UITableView does not automatically update cell heights unless explicitly said to do so.

The solution, if you really have to do this, is to calculate the height of the outer cell correctly before it is displayed and manually set a height constraint.

If you know the height (from data source), it's easy. If you actually need to calculate the height of the inner table, you can do something like this:

// make the table high enough to display all cells
innerTableHeightConstraint.constant = 2000
// reload table
innerTable.reloadData()
// force layout
innerTable.layoutIfNeeded()
// now the contentSize is correctly calculated
innerTableHeightConstraint.constant = innerTable.contentSize.height

The whole concept is tricky and ideally you should prefer using UICollectionView or table sections. When you are using inner table views, there won't be any cell reuse for the inner tables and your performance will suffer.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • In which methos donyou call this piece of code, in the viewDidLoad()? – BasM May 12 '19 at 07:51
  • @BasM Inside the tableviewcell, everytime you change the data source that can affect the height. Make sure you understand the long text above before you skip to code. – Sulthan May 12 '19 at 08:04
  • you mean inside the cellForRowAt? – BasM May 13 '19 at 07:04
0

You should not wrap UITableView into UITableViewCell, try to use UITableView Sections instead to add an extra depth level to your UITableView.

Manoj Rlogical
  • 239
  • 1
  • 4
iDevid
  • 507
  • 3
  • 13
  • I already use the sections, what do you propose if you really need that extra depth? – BasM May 10 '19 at 11:22
  • Actually seems that you're using one section for cell, you can use multiple sections to get the same behaviour – iDevid May 10 '19 at 12:29