Dynamic UITableView Cell Height Based on Contents

前端 未结 12 705
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 07:29

I have a UITableView that is populated with custom cells (inherited from UITableViewCell), each cell contains a UIWebView that is auto

12条回答
  •  我在风中等你
    2020-12-05 07:43

    The best way that I've found for dynamic height is to calculate the height beforehand and store it in a collection of some sort (probably an array.) Assuming the cell contains mostly text, you can use -[NSString sizeWithFont:constrainedToSize:lineBreakMode:] to calculate the height, and then return the corresponding value in heightForRowAtIndexPath:

    If the content is constantly changing, you could implement a method that updated the array of heights when new data was provided.

提交回复
热议问题