Swift - UITableView - Dynamic height of cells

Hello everyone,

I have been struggling a lot with this issue:
Using an UITableView, I want to use a custom UITableViewCell which contains labels and an image view.

Depending on how long the text is, which I want to display on the labels, the height of the cell should be adjusted automatically.

Currently, I have this approach in my viewDidLoad method:

Code Block
  activityTableView.translatesAutoresizingMaskIntoConstraints = false
    self.view.addSubview(activityTableView)
    activityTableView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
    activityTableView.widthAnchor.constraint(equalToConstant: width).isActive = true
    activityTableView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: height * (60 / IPHONE8_SCREEN_HEIGHT)).isActive = true
    activityTableView.heightAnchor.constraint(equalToConstant: height * (554 / IPHONE8_SCREEN_HEIGHT)).isActive = true
    //activityTableView.frame = CGRect(x: 0, y: navigationBar.frame.maxY, width: width, height: height * (554 / IPHONE8_SCREEN_HEIGHT))
    activityTableView.delegate = self
    activityTableView.dataSource = self
    activityTableView.register(ActivityTableViewCell.self, forCellReuseIdentifier: "activityCell")
      activityTableView.estimatedRowHeight = 92
    activityTableView.rowHeight = UITableView.automaticDimension
     

Furthermore, I have delegates:
Code Block
extension ActivityViewController:UITableViewDelegate {
  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
  }  
}

My custom table view cell
Code Block  
func initActivityLabel() {
    activityLabel.translatesAutoresizingMaskIntoConstraints = false
    self.contentView.addSubview(activityLabel)
    activityLabel.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 81).isActive = true
    activityLabel.widthAnchor.constraint(equalToConstant: cellWidth * (276 / cellWidth)).isActive = true
    activityLabel.topAnchor.constraint(greaterThanOrEqualTo: self.contentView.topAnchor, constant: 13).isActive = true
    activityLabel.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -38).isActive = true
     
    activityLabel.setColorForLabelTitle()
    activityLabel.font = .boldSystemFont(ofSize: LABEL_TEXT_SIZE)
    activityLabel.numberOfLines = 0
    activityLabel.lineBreakMode = .byWordWrapping
    activityLabel.sizeToFit()
  }
   
  func initActivityButton() {
    self.activityButton.translatesAutoresizingMaskIntoConstraints = false
    self.contentView.addSubview(activityButton)
     
    activityButton.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 250).isActive = true
    activityButton.widthAnchor.constraint(equalToConstant: 131).isActive = true
    activityButton.topAnchor.constraint(equalTo:self.activityLabel.bottomAnchor, constant: 0).isActive = true
    activityButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
   
    activityButton.addTarget(self, action: #selector(activityButton_clicked), for: .touchDown)
    activityButton.setColor()
    activityButton.roundCorners()
    self.layoutIfNeeded()
  }

Those init-functions are called within this method
Code Block  
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let activityCell = tableView.dequeueReusableCell(withIdentifier: "activityCell", for: indexPath) as! ActivityTableViewCell
    activityCell.selectionStyle = .none
    activityCell.sizeToFit()
   
    activityCell.backgroundColor = DARK_BLUE_COLOR
    let activityObject = activityArray[indexPath.row]
     
activityCell.initActivityLabel() ..


As you can see, everything is done programmatically. I need to do it like this, so I was wondering if you could give me any hints.

When I run the code, the table view doesn't show the label at all. I get a warning regarding a constraint violation. But why?


Swift - UITableView - Dynamic height of cells
 
 
Q