How can I adjust the UITableViewCell height to the content of UITextView that's inside?

前端 未结 1 1459
隐瞒了意图╮
隐瞒了意图╮ 2021-01-17 02:56

In my story board I have a UITableView with dynamically generated UITableViewCells. Each cell contains 2 labels and 1 text view:

I

相关标签:
1条回答
  • 2021-01-17 03:07

    The key to getting self-sizing table cells (autolayout-based, which I recommend) is as follows:

    • Add your subviews to the contentView of the UITableViewCell
    • Provide constraints between your subviews and the contentView such that your subviews reach all edges of the table cell. In your case, this probably means aligning the leading, trailing, top, and bottom edges of your UITextView to the corresponding edges of the contentView.
    • Set the row height to UITableViewAutomaticDimension instead of a hardcoded CGFloat.
    • Somewhere in your controller, provide an estimation of the height with tableView.estimatedRowHeight = x (a hard coded constant is fine, this is for performance).
    0 讨论(0)
提交回复
热议问题