Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set dynamic height of cell based on content

I am have an app which requires to display user comments within a table cell. How can I set the dynamic height of the cell based on the contents of the cell like in Instagram?

enter image description here

Basically, I have texts and comments that have different length in different cells. Can anyone advise me on how I can implement the dynamic height method (like instagram)?

like image 700
Zhen Avatar asked Jun 14 '11 14:06

Zhen


3 Answers

Check the UITableViewDelegate documentation.

- (CGFloat)tableView:(UITableView *)tableView 
                                heightForRowAtIndexPath:(NSIndexPath *)indexPath

Bear in mind you can't reference the cell from this method, so you want to calculate the height of the contents separately and use it to generate the height.

like image 124
simon aplin Avatar answered Oct 08 '22 20:10

simon aplin


Combine my dynamic label category here with my dynamic table cell height here. Perfect combination!

like image 3
Simon Lee Avatar answered Oct 08 '22 18:10

Simon Lee


You should give a look at three20 library source code, which offers complex cells like you want to do. It might give you answers on how to do this, even if you do not intend to use it:

three20 Cells

like image 1
Vincent Guerci Avatar answered Oct 08 '22 20:10

Vincent Guerci