views:

318

answers:

2

Hi Folks,

I have a problem in an iPhone application.

Application has a table view controller with custom table view cells. Each cell has a Label (please correct me if I need to use text view etc). I am getting text dynamically from a web service call and I don't know how long text gonna be.

Now problem is that I want to adjust the table view cell height based on text I receive. How can I grow Label or TextView height withinin table view cell so it can contain all the text and in effect grow table view cell height.

Does anyone know how to handle this kind of design problem?

Thanks

A: 

You can get the height of the string passed from the below function

-(float)getHeightByWidth:(NSString*)myString:(UIFont*)mySize:(int)myWidth {

    CGSize boundingSize = CGSizeMake(myWidth, CGFLOAT_MAX);
    CGSize requiredSize = [myString sizeWithFont:mySize constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];

    return requiredSize.height;

}

Based on the above function you can set the height of the label as well as the cell where its gonna use..

Suriya
A: 

The answer depends on the size and complexity of the information your displaying.

For small amounts of text, you can use the NSString UIKit Additions to calculate the dimensions of the displayed string and then adjust the size of labels or other controls of a cell before returning the cell to the tableview. This will let you fine tune the display for each cell.

For larger amounts of text I suggest a detail view if at all possible. Display a small amount of text in the table and then have the selection of the table open another view containing the full text. I think it difficult for most users to read a lot of text in a tableview.

TechZen
Yeah I am also considering this desing logic. Text in my case wont be greater than 500 characters. What do you think in this case?
Leo
If the size of the table view takes up a third or more of the vertical screen area, it stops looking like a table and users get confused on how to operate it. I think you have to have at least three visible cells at all times. An alternative is to display text in a detail view that pages left and right like the photo library detail view. It all depends on the actual data and how people are going to use it.
TechZen