无法为UITableView提供自动高度

时间:2017-08-07 07:31:57

标签: ios swift uitableview

我的代码中有以下UITableView

tableView = UITableView(frame: CGRect(x:0,y:0,width:0,height:0))
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellReuseIdentifier")
tableView.delegate = self
tableView.dataSource = self
tableView.translatesAutoresizingMaskIntoConstraints = false

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return comments.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "cellReuseIdentifier") as UITableViewCell!

    cell.textLabel?.text = self.comments[indexPath.row]
    return cell
}

tableView.topAnchor.constraint(equalTo:bottomView.bottomAnchor).isActive = true
tableView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
tableView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true

我还被建议添加底部锚点,但我不知道在哪里添加它们,我想给这个UITableView一个自动高度。我尝试了两件事

tableView.estimatedRowHeight = 88.0
tableView.rowHeight = UITableViewAutomaticDimension
tableView.heightAnchor.constraint(equalToConstant: tableView.contentSize.height).isActive = true

他们都没有工作。

0 个答案:

没有答案