У меня есть основной UITableView с одной ячейкой, которая содержит две метки (заголовок, описание) и два UITableView (imagesTableView, subTextsTableView).Тип subTextsTableView совпадает с типом основного tableView, что означает, что он также может иметь метки и imagesTableView.Моя проблема в том, что я не могу получить желаемую высоту основного tableView.
Вот рисунок, который ясно описывает мою проблему.Все ячейки tableView должны быть видимыми
https://imgur.com/a/IjDQZCl
Вот код
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
}
}