0

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

video showing the issue

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

Carlos Padron
  • 53
  • 1
  • 5
  • You can better use autolayout to calculate the cell height automatically and remove this method `func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {`. – Kamran Sep 21 '20 at 04:01
  • Sorry for not being clear enough, i updated the post. – Carlos Padron Sep 21 '20 at 18:53

1 Answers1

1

I fixed it by replacing

cell.layoutIfNeeded()
cell.updateConstraintsIfNeeded()
cell.layoutSubviews()

with

cell.layoutIfNeeded()
cell.layoutSubviews()
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()
Carlos Padron
  • 53
  • 1
  • 5