0

I have an issue which is bothering me for a while. How can I set an automatic height for a tableview which is inside another tableview cell?

So I'm having MainTableView with 3 cells inside it (this tableview has automatic height) and on the 3rd cell I have another tableView inside it (let's call it SecondaryTableView which has constraints to its superview)

Image1

Image2

Thing is for the first 2 cells the automatic height it's working.

For the setups of the SecondaryTableView I've tried to do the same but for the cells I've opted for xib files.

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        tableView.dataSource = self
        tableView.delegate = self
        tableView.estimatedRowHeight = 900
        tableView.rowHeight = UITableView.automaticDimension
        registerCells()
    }

    private func registerCells() {
        tableView.register(UINib(nibName: "MainCategorySectionTableViewCell", bundle: nil), forCellReuseIdentifier: "MainCategorySectionTableViewCell")
        tableView.register(UINib(nibName: "AdPerHomePageTableViewCell", bundle: nil), forCellReuseIdentifier: "AdPerHomePageTableViewCell")
    }

I've attached one more picture of how it looks in the simulator. The green color is the first cell and the orange one the second ( those registered). I've scrolled a little for the first 2 cells to see the difference.

Output

Might it be that no data is on the cell? But I've put some labels and it looks the same

chirag90
  • 2,211
  • 1
  • 22
  • 37
Mohamed Lee
  • 267
  • 1
  • 3
  • 18

2 Answers2

3

You need to use intrinsicContentSize variable of a UITableView.

Create a subclass for child tableView and override intrinsicContentSize.

Dynamic row heights of a UITableView inside a UITableViewCell

Hitesh Borse
  • 479
  • 2
  • 12
  • Solved my issue. Thanks a lot – Mohamed Lee Sep 18 '19 at 10:48
  • hi hitest ,i have UITableview inside tableview cell i have ui like Heading ->subHeading ->Detail when i click on heading it shows subheading when tap on subheading it shows details but when i fast scrolling details are not visible but normally it works fine – Aqib Zareen Nov 21 '21 at 09:06
2

You might need a closure at each cells which contain tableView, the closure is invoked as soon as the tableView completed reloading data. From you parent tableView, just capture those closures then reload the whole tableView (or corresponding section).

In your main ViewController

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? YourTableViewCell else {
            return UITableViewCell()
        }
        cell.onCompleteReloading = { [weak self] in 
            guard let self = self else { return }
            tableView.reloadSections([indexPath.section], animationStyle: .none)
        }
        cell.setup()
        return cell
    }

In your Cell, define a closure

var onCompleteReloading:  (() -> Void)?

func setup() {
    // Do some stuff here
    // Then reload tableView
    self.reloadTableView()
}

func reloadTableView() {
    self.tableView.reloadData()
    self?.onCompleteReloading()
}
jacob
  • 1,024
  • 9
  • 14