Reputation: 2501
I have a dynamic table view inside a static table view. I am not able to change the cell height according to the cell content. I have tried various methods like
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (ProfileData.profileViewType == .favourite) {
if let cell = tableView.dequeueReusableCell(withIdentifier: "profileCell", for: indexPath) as? ProfileCell { // gives EXC_BAD_ACCESS in SimpleCheckbox library
cell.favouritesTitleLabel.numberOfLines = 0
cell.favouritesTitleLabel.sizeToFit()
print(cell.favouritesTitleLabel.text)
let height = cell.favouritesTitleLabel.text!.height(withConstrainedWidth: cell.frame.width - 64, font: UIFont(name: "Roboto", size: 17.0)!)
print(height + 16)
return height + 16
}
}
return UITableView.automaticDimension
}
I have tried setting UITableView.automaticDiemension
, but it is not adjusting to the text view. It is getting larger. If I try tableView.dequeueReusableCell(withIdentifier: "profileCell") as! ProfileCell
I am getting only the contents of two cells and rest are not getting the text value.
The current UI screenshot is: UI screenshot
I have set the following table view config in viewDidLoad()
:
profileTableView = ProfileSectionTableView()
profileTableView.profileDelegate = self
profileSectionTableView.delegate = profileTableView
profileSectionTableView.dataSource = profileTableView
profileSectionTableView.rowHeight = UITableView.automaticDimension
profileSectionTableView.estimatedRowHeight = 44
Upvotes: 1
Views: 425
Reputation: 136
When you called the func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath)
,you must set cellData
again but get data with cell. Then calculate the height.
Upvotes: 1