0

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
            
Jawad Ali
  • 13,556
  • 3
  • 32
  • 49
multiverse
  • 425
  • 3
  • 13
  • what error do you get no setting detailTableView.estimatedRowHeight = 40 – Muhammad Ali Jul 05 '20 at 06:39
  • 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 where – multiverse Jul 05 '20 at 06:44
  • you might be setting row hight to zero in storyboard chek this link if this is true https://stackoverflow.com/questions/37651967/xcode-storyboard-where-to-set-uitableviewcell-height – Muhammad Ali Jul 05 '20 at 06:46
  • what you are returning in `heightForRow`? – Jawad Ali Jul 05 '20 at 06:46
  • Does this answer your question? [Using Auto Layout in UITableView for dynamic cell layouts & variable row heights](https://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights) – Asif Mujtaba Jul 05 '20 at 06:47
  • @jawadAli , not using. that method, but even when i added it and added the code detailTableView.rowHeight = UITableView.automaticDimension detailTableView.rowHeight = 40 , no effect – multiverse Jul 05 '20 at 06:48

1 Answers1

1

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
    }
Jawad Ali
  • 13,556
  • 3
  • 32
  • 49
  • thanks, i added the function but still no effect, the only line that seems to have an effect is this, detailTableView.rowHeight = 40, if i remove it i get error that height might be zero, i checked my constraints and still same issue, i must be missing some lines of code some where – multiverse Jul 05 '20 at 07:10
  • please share demo project to check – Jawad Ali Jul 05 '20 at 07:28
  • i added entire code where the changes are attempted, other then in viewDidLoad and heightForRowAtIndexPath as. you had mentioned – multiverse Jul 05 '20 at 11:09
  • why are you adding cell in view ? – Jawad Ali Jul 05 '20 at 11:17
  • what these lines view.addSubview(cell.descriptionDetail) view.addSubview(cell) are doing ? – Jawad Ali Jul 05 '20 at 11:18
  • they add the views to the original view, if i delete these lines some errors which say not inherited from same parent appear – multiverse Jul 05 '20 at 12:48