2017-08-06 76 views
0

我有我的代码如何给动态高度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 
} 

一个UITableView,我想给一个动态的高度,这UITableView.I试过两件事情

tableView.estimatedRowHeight = 88.0 
    tableView.rowHeight = UITableViewAutomaticDimension 

    tableView.heightAnchor.constraint(equalToConstant: tableView.contentSize.height).isActive = true 

,没有他们工作。

回答

0

你并不需要使用下面的一行代码一旦设定了UITableViewAutomaticDimension

tableView.heightAnchor.constraint(equalToConstant: tableView.contentSize.height).isActive = true 

的问题可能是你的autolayout约束这似乎并不适当。因此,您需要确保在tableViewCell对象内正确固定了前导,尾随,顶部和底部约束。

注: - 如果你正在使用的UILabel然后设置的行数= 0

+0

我只有顶部,左,右的约束,而不底部'UITableView'和细胞内的,我不除文本外,还有其他任何内容 – sakoaskoaso

+0

您还需要设置底部约束。否则,你的tableViewcell将不能正确调整大小。 –

+0

但这会自动定义它的高度不是吗?我试图将它的底部固定到它的父级UIView,并将它调整到父级的高度 – sakoaskoaso

相关问题