0

This may be a simple question but i wish to through the correct path.

Im fetching some contents from the webservice and displaying it in a table view.

The length of text that is fetching from there may vary.

Sometime it may have only one line sentence,sometime a paragraph etc.

So my doubt is how can i vary the height of custom cell according to the content coming from webservice.

Hoping for your help.

Thanks in advance.

suji
  • 1,470
  • 6
  • 22
  • 41
  • same question like this :-http://stackoverflow.com/questions/1443335/how-can-i-do-variable-height-table-cells-on-the-iphone-properly – Leena Jan 30 '12 at 09:21

2 Answers2

0

In you UITableViewDelegate implement the following method

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

returning the proper height according to the row.

you can use the method -sizeWithFont: of NSString to get the pixel size of the text you get.

http://developer.apple.com/library/IOs/documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html#//apple_ref/occ/instm/NSString/sizeWithFont:

Jorge Cohen
  • 1,512
  • 10
  • 34
0

Let's assume you store the text for each content in an array. And the index lined up with the table cell. Here is the code you want to use.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *string = [array objectAtIndex:indexPath.row];

//300 is the width, you can change this to make it look right.
CGSize maximumLabelSize = CGSizeMake(300 ,9999);
CGSize expectedLabelSize = [string sizeWithFont:YOURLABEL.font 
                                    constrainedToSize:maximumLabelSize 
                                        lineBreakMode: UILineBreakModeWordWrap]; 

return expectedLabelSize.height;

}

Pretty much covers the basic. Just ask if you have any more questions. And read Leena's comment. It is pretty spot on.

Byte
  • 2,920
  • 3
  • 33
  • 55