Swift dynamic table cell height [duplicate]

孤人 提交于 2019-12-28 05:56:46

问题


I have tableview with prototype cell, in the cell I have imageview and some text under. The text label is one laine in the prototype cell, but sometimes it is more than one line, I'm loading the data to the table view after server call. The label in storyboard Lines are set to 0 and Line Break to Word Wrap. I also tried http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/

but no effect. If I use Using Auto Layout in UITableView for dynamic cell layouts & variable row heights everything works because the label text is predefined, but I'm loading the data from server and I reload the tableView after the API call, and then the label is just one line.


回答1:


You should use UITableViewAutomaticDimension provides a solution for displaying dynamic content.

Use below code in viewDidLoad:

tableView.estimatedRowHeight = YourTableViewCellHeight
tableView.rowHeight = UITableView.automaticDimension

Read more from here

Hope it helps. :)




回答2:


make sure UILabel height constraint not to be constant, if constant it have to set Relation greater than or equal to from attribute inspector

make sure UITableViewestimate row height set as per your.

self.TableName.estimatedRowHeight = 85

and write in UITableViewCell class

 Cell.lbl_name.sizeToFit()


 Cell.lbl_name.numberOfLines = 0

Make sure your UITableViewDelegate method

func tableView(tableView: UITableView,
    heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{

 return UITableViewAutomaticDimension
}


来源:https://stackoverflow.com/questions/36574052/swift-dynamic-table-cell-height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!