Swift UITableViewAutomaticDimension不工作
我有这张桌子和手机Swift UITableViewAutomaticDimension不工作,swift,uitableview,Swift,Uitableview,我有这张桌子和手机 tableView = UITableView() tableView.dataSource = self tableView.delegate = self tableView.allowsSelection = false; tableView.estimatedRowHeight = 200 tableView.rowHeight = UITableViewAutomaticDimension var postTexts = ["Nirvana"] var pos
tableView = UITableView()
tableView.dataSource = self
tableView.delegate = self
tableView.allowsSelection = false;
tableView.estimatedRowHeight = 200
tableView.rowHeight = UITableViewAutomaticDimension
var postTexts = ["Nirvana"]
var posters = ["Champagnepapi"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return postTexts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let ProfilePicture = UIImageView(frame: CGRect(x:19, y: 14, width: 50, height: 50))
ProfilePicture.image = #imageLiteral(resourceName: "cole")
ProfilePicture.layer.cornerRadius = 25
ProfilePicture.layer.masksToBounds = true
let username = UILabel(frame: CGRect(x: 80, y: 14, width: 300, height: 30))
username.text = posters[indexPath.row]
let postText = UITextView(frame: CGRect(x:75,y:40,width:200,height:200))
postText.text = "I rather be dead than cool - Kurt Cobain"
let border = UIView(frame: CGRect(x:0,y:150,width:350,height:1))
border.backgroundColor = UIColor(r: 217, g: 221, b: 219)
cell.addSubview(ProfilePicture)
cell.addSubview(username)
cell.addSubview(postText)
cell.addSubview(border )
return cell
}
我希望我的手机有自动高度,但这是我得到的结果
如何获得单元格的自动高度?您需要像这样设置单元格对象的约束,以便单元格可以计算所需的大小
使用故事板的可能重复将使这更容易,因为您需要使用约束来设置单元格。诀窍是确保单元格中的对象约束单元格的所有边(顶部、前导、尾部和底部)。如果不这样做,autolayout将无法计算单元格的大小。@rmp我更喜欢编写代码,而且我的所有项目都已经编写好了。这很好,您只需要以编程方式为所有单元格对象编写约束。@rmp高度约束?谢谢homey。您的回答帮助了我,希望能帮助后代开发应用程序,实现梦想。