UITableViewCell, UITextView with dynamic height

前端 未结 3 1286
孤城傲影
孤城傲影 2020-12-02 16:04

I need to make a UITableViewCell that holds alot of text. I know I can add a UITextView to my cell, but each entry will not have the same amount of

3条回答
  •  日久生厌
    2020-12-02 16:16

    The solution is quite simple and should work since iOS 7. Make sure that the Scrolling Enabled option is turned off for the UITextView inside the UITableViewCell in the StoryBoard.

    Then in your UITableViewController's viewDidLoad() set the tableView.rowHeight = UITableViewAutomaticDimension and tableView.estimatedRowHeight > 0 such as:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 44.0
    }
    

    That's it. UITableViewCell's height will be automatically adjusted based on the inner UITextView's height.

提交回复
热议问题