How to resize UITableViewCell to fit its content?

前端 未结 8 762
有刺的猬
有刺的猬 2020-12-08 05:34

I have a UITableview with multiple reusable TableViewCells. In one cell I have a UITextView, that resizes itself to fit its content. Now I \"just\

8条回答
  •  无人及你
    2020-12-08 05:37

    This thread has been quite a while, but in iOS 8 UITableViewAutomaticDimension was introduced. You have to set constraints from the top to the bottom of the cell like a scroll view to make this work. But after that, just add the following code to viewDidLoad():

    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 122.0
    

    Make sure your estimated height is as close as possible to the real thing otherwise you'll get some buggy scrolling.

提交回复
热议问题