Tableview in UITableviewcell

Source: Author

# Use this custom TableView size

Step one create table view and custom table view cell, in that cell there is another table view, the following is how to make the table in the cell auto height. add the following custom table into the custom class.

import UIKitclass FirstTableViewCell: UITableViewCell { @IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var tableView: UITableView!
override func awakeFromNib() {
super.awakeFromNib()
setupTableView()
} private func setupTableView() {
tableView.dataSource = self
tableView.rowHeight = UITableView.automaticDimension
tableView.register(UINib(nibName: "SecondTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
tableView.separatorColor = .darkGray
tableView.separatorInset = UIEdgeInsets.zero
}
func reloadData() {
DispatchQueue.main.async {
self.tableView.reloadData()
self.tableView.layoutIfNeeded()
}
}
}extension FirstTableViewCell: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! SecondTableViewCell
cell.titleLabel.text = "Sub Table Cell \(indexPath.row)"
cell.backgroundColor = .blue.withAlphaComponent(0.5)
return cell
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store