0
votes

I use below code to add constraints programatically, no story board used

cell.descriptionDetail.leadingAnchor.constraint(equalTo: cell.contentView.leadingAnchor, constant: 15).isActive = true

            cell.descriptionDetail.topAnchor.constraint(greaterThanOrEqualTo: cell.contentView.topAnchor, constant: 20).isActive = true

cell.descriptionDetail.trailingAnchor.constraint(equalTo: cell.contentView.trailingAnchor, constant: -20).isActive = true

            cell.descriptionDetail.bottomAnchor.constraint(greaterThanOrEqualTo: cell.contentView.bottomAnchor, constant: 10).isActive = true

Now i use this in viewDidLaod

 detailTableView.rowHeight = UITableView.automaticDimension
    
        detailTableView.rowHeight = 40

even if i remove height constraints on all cell the row height does. not adjust, if i remove

 detailTableView.rowHeight = 40

and add detailTableView.estimatedRowHeight = 40, i get error, currently this is what happens , content overlapping cells

enter image description here

UPDATE MY ENTIRE CODE OF FILE WHERE THE CELL IS BEING CREATED AND CONSTRAINED

let cell = detailTableView.dequeueReusableCell(withIdentifier: String(describing: TextOnlyCell.self), for: indexPath) as! TextOnlyCell
            view.addSubview(cell.descriptionDetail)
            view.addSubview(cell)
     
            cell.descriptionDetail.numberOfLines = 0
            cell.descriptionDetail.lineBreakMode = .byWordWrapping
           
            
           
            cell.descriptionDetail.translatesAutoresizingMaskIntoConstraints = false
            cell.descriptionDetail.widthAnchor.constraint(greaterThanOrEqualToConstant: 100).isActive = true
            cell.descriptionDetail.heightAnchor.constraint(greaterThanOrEqualToConstant: 20).isActive = true

 ///Constraints
            
            cell.descriptionDetail.leadingAnchor.constraint(equalTo: cell.leadingAnchor, constant: 15).isActive = true

            cell.descriptionDetail.topAnchor.constraint(greaterThanOrEqualTo: cell.topAnchor, constant: 10).isActive = true

            cell.descriptionDetail.trailingAnchor.constraint(equalTo: cell.trailingAnchor, constant: -5).isActive = true

            cell.descriptionDetail.bottomAnchor.constraint(greaterThanOrEqualTo: cell.bottomAnchor, constant: 10).isActive = true
            
          
        
            cell.descriptionDetail.text = restaurant.description
            
            
            return cell
            
1
what error do you get no setting detailTableView.estimatedRowHeight = 40Muhammad Ali
no error for removing it , problem remains, but if i remove detailTableView.rowHeight = 40, then i get waring that i am setting some rowheight to 0 some wheremultiverse
you might be setting row hight to zero in storyboard chek this link if this is true stackoverflow.com/questions/37651967/…Muhammad Ali
what you are returning in heightForRow?Jawad Ali

1 Answers

1
votes

Add this method in your class... and be sure that your constraints are attached with top and bottom ... to help automaticDimension to calculate height

  func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }