Swift UITableViewAutomaticDimension is not working

前端 未结 4 921
时光取名叫无心
时光取名叫无心 2021-01-28 18:03

I have got a table in my Swift project like this

var tableView: UITableView! 
tableView = UITableView()
tableView.dataSource = self
tableView.delegate = self
ta         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-28 18:30

    I had problems with constraints so here is what i did.
    First i added heightForRowAt

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
    

    Then i created a variable

    let tableViewHeight  = postTexts.count*100
    

    And put that in my height constraint

        tableView.anchor(PostsDiv.bottomAnchor, left:view.leftAnchor, bottom: nil, right: view.rightAnchor, topConstant: 40, leftConstant: 15, bottomConstant: 0, rightConstant: 15, widthConstant: 0,heightConstant: CGFloat(tableViewHeight))
    

    And it works pretty well for me

提交回复
热议问题