I'm having some issue with the table vie cells, only after some scrolling the cells get their right height. I tried some answers from this post Text. Added layoutIfNeeded() but i doesn't seem to work
My Code:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView === MainTable{
if let cell = tableView.dequeueReusableCell(withIdentifier: "MedName", for: indexPath) as? MedCell{
let backgroundView = UIView()
backgroundView.backgroundColor = UIColor.white.withAlphaComponent(0.0)
cell.selectedBackgroundView = backgroundView
cell.setMedName(name: self.medCatalog[indexPath.row].nombre, uso: self.medCatalog[indexPath.row].uso )
cell.layoutIfNeeded()
cell.updateConstraintsIfNeeded()
cell.layoutSubviews()
self.cellBool[indexPath.row] = true
self.collapsedCellHeight[indexPath.row] = cell.medText.bounds.height
return cell
}
}
self.cellBool[indexPath.row] = false
self.collapsedCellHeight[indexPath.row] = 0.0
self.expandedCellHeight[indexPath.row] = 0.0
return UITableViewCell()
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if self.cellBool[indexPath.row] != nil {
if self.cellBool[indexPath.row]!{
let nameHeight = self.collapsedCellHeight[indexPath.row]!
let topAnchor: CGFloat = 12.0
let bottomAnchor: CGFloat = 10.0
let cellHeight = nameHeight + topAnchor + bottomAnchor + 9 + 2
return cellHeight
}
}
return 85
}
EDIT
I forgot to mention that the main goal to make an expandable and collapsible row, that why there's a UIlabel below the blue UIlabel.
The blue uilabel text is gotten from a json. Some values from the json are a bit long so the uilabel create another line to show all the content. That's why i need to provide the cell height, because some labels my have 1 line and other more than one line and i need to calculate the blue label's height and on didSelectRowAtIndexPath
expand the entire cell. I know how to expand and collapse de row what i don't know is why the table behave like so