Sagar...
Sagar...

Reputation: 1062

Adding UITableView inside UITableViewCell. TableView height making based on content

I have created UITableViewCell which contains with following hierarchy

UITableViewCell ------ ContentView ------------ScrollView ------------------TableView

Scrolling on TableView is stopped.

I have taken outlet of height constraint of TableView in custom class in which table's delegate and datasource is implemented.

Up till this point data is visible with one issue. [ Table height is fixed which is set to 50 px via constraint from xib ]

Now inside willDisplay call I am updating height constraint to tableview's content size. But this is not getting updated immediately. Once I scroll the table View it show table with updated height.

Have debugged around constraint's constant which is showing correctly but UI is showing old height though I have called reloadData on tableview. anything missing here...

GitHub Link : https://github.com/sagarmane19/Sample-Apps

Upvotes: 1

Views: 1358

Answers (1)

alexburtnik
alexburtnik

Reputation: 7741

1.Create a custom class for inner tableView with the following override:

class InnerTableView: UITableView {
    override var intrinsicContentSize: CGSize {
        return self.contentSize
    }
}

2.Return UITableViewAutomaticDimension in heightForRow method in your view controller:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

3.Make sure you have top and bottom constraints for cell's inner tableView.

Upvotes: 4

Related Questions