0

I have main UITableView with one cell, which contains two labels (title, description) and two UITableViews(imagesTableView, subTextsTableView). subTextsTableView is same type as main tableView, which means that it may also have labels and imagesTableView. My problem is that i can't get desired height of main tableView.

Here is a gif which describes my problem clearly. Every tableView cells should be visible

https://i.stack.imgur.com/2JH5S.jpg

Here is code

   class BodyTextTableViewCell: UITableViewCell {

    @IBOutlet weak var titleLabel: GRELabel!
    @IBOutlet weak var descriptionLabel: GRELabel!
    @IBOutlet weak var imagesViewHeightConstraint: NSLayoutConstraint!
    @IBOutlet weak var imagesTableView: BodyTextImagesTableView!
    @IBOutlet weak var subTextsTableView: BodyTextsTableView?
    @IBOutlet weak var subTextsViewHeightConstraint: NSLayoutConstraint?

    override func prepareForReuse() {
        titleLabel.text = nil
        descriptionLabel.text = nil
        imagesViewHeightConstraint.constant = 0
        imagesTableView.data = nil
        subTextsViewHeightConstraint?.constant = 0
        subTextsTableView?.data = nil
    }

}

class BodyTextImageTableViewCell: UITableViewCell {

    @IBOutlet weak var titleLabel: GRELabel!
    @IBOutlet weak var coverImageView: UIImageView!

    override func prepareForReuse() {
        titleLabel.text = nil
        coverImageView.image = nil
    }

}

class BodyTextsTableView: UITableView, UITableViewDataSource {

    var data: [BodyText]?

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        rowHeight = UITableView.automaticDimension
        dataSource = self
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data?.count ?? 0
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BodyTextTableViewCell") as! BodyTextTableViewCell
        cell.titleLabel.text = data?[indexPath.row].title
        cell.descriptionLabel.text = data?[indexPath.row].description
        cell.imagesTableView.data = data?[indexPath.row].images
        cell.imagesTableView.reloadData {
            cell.imagesViewHeightConstraint.constant = cell.imagesTableView.contentSize.height
        }
        cell.subTextsTableView?.data = data?[indexPath.row].subTexts
        cell.subTextsTableView?.reloadData {
            cell.subTextsViewHeightConstraint?.constant = (cell.subTextsTableView?.contentSize.height ?? 0)
        }
        return cell
    }

}

class BodyTextImagesTableView: UITableView, UITableViewDataSource {

    var data: [BodyTextImage]?

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        dataSource = self
        rowHeight = UITableView.automaticDimension
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data?.count ?? 0
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BodyTextImageTableViewCell") as! BodyTextImageTableViewCell
        cell.titleLabel.text = data?[indexPath.row].title
        if let url = data?[indexPath.row].url, let with = URL(string: url) {
            cell.coverImageView.kf.setImage(with: with)
        }
        return cell
    }

}
AjinkyaSharma
  • 1,870
  • 1
  • 16
  • 26

0 Answers0