views:

74

answers:

3

Hi all,

I want to calculate my custom cell height with multiple subviews like labels, imageViews, buttons etc.

How do I calculate this cell size?

It seems it's not recommended to do it in cellForRowAtIndxPath, also taking an instance of Cell in heightForRowAtIndexPath, it goes in infinite loop.

I'm able to calculate the height for cell in the custom cell class but I'm not sure wheather where should I return it.

Since the method, heightForRowAtIndexPath doesn't get a cell instance, how to tell this method the type of cell for which I want to render the calculated height?

Any help is appreciated.

Thanx in advance.

A: 

Ideally, you should be able to calculate the height of the cell without having to create a cell for it. tableView:heightForRowAtIndexPath: will be called for every cell in your table (not just the ones on screen) and if you have to create a complete cell object in that method it will probably have a big impact on performance.

You have to derive the type of cell based on the indexPath argument, in the same way you do it in tableView:cellForRowAtIndexPath:

Ole Begemann
I've cells of different types with many subViews. Can you suggest any ideal location to calculate the size of differnt cells? Do I have to use the cell identifiers to distinguish them? I tried setting cell.frame to be CGRectZero but it doesn't work for me..
neha
You have no choice but to calculate the height in `tableView:heightForRowAtIndexPath:`. If you also need the values to set the heights of the elements of your cell, you should put the calculation in a separate method. If calculating the height in both `tableView:heightForRowAtIndexPath:` and `tableView:cellForRowAtIndexPath:` impacts your performance, you should cache the values calculated in `tableView:heightForRowAtIndexPath:`.
Ole Begemann
Ok, agreed.. In this case I can calculate the cell height by taking all its subviews and accumulating their heights which I can do, but for that I need a UITableViewCell instance. How can I do that? Using cell = [tableView cellForRowAtIndexPath:indexPath]; makes it an infinite loop. If I am able to do this, then even I won't be needed to go for the "distinguish cells" part. Thanx.
neha
Thanx, Ole.. i helped..
neha
A: 

You could always load your cells in viewWillAppear or viewDidLoad, and then add them to an NSArray. Then, in tableView:heightForRowAtIndexPath:, using the provided indexPath, return the height of the relevant cell in your array. Then, in tableView:cellForRowAtIndexPath, again, using the provided indexPath, return the relevant cell in your array.

PLEASE NOTE:
If you have a lot of cells, this could cause your UI to lock up while it loads the cells. To avoid this, you could create a custom method to populate the array, and perform it on a background thread. Also, if it takes too long, the tableView will not display all of your cells, as your array will not have been fully populated. This means that you should call [tableView reloadData]; after the array is populated.

jrtc27
Do I have to use the cell identifiers to distinguish cells in heightForRowAtIndexPath? How can take an instance of cell in this method?
neha
You do not need to use cell identifiers to distinguish the cells. Instead, use the `indexPath` that `tableView:heightForRowAtIndexPath` and `tableView:cellForRowAtIndexPath` provide - access the row and section properties of the `indexPath` and access the relevant cell from the array using `objectAtIndex:`.
jrtc27
Thanx jrtc27.. it helped..
neha
A: 

This' how I did it:

I created an array of catgories of the cells in the order of their apearence in tableview called category_type and then I checked for the category types and calculated the height of the text-based controls to calculate the entire height of cell.

NSString *category = [NSString stringWithFormat:[category_type objectAtIndex:indexPath.row]];

if([category isEqualToString:@"xyz"])
{
    xyzInstance = [arrayWithObjectsToLoad objectAtIndex:indexPath.row];


    float textHeight = [MLUtils calculateHeightOfTextFromWidth:editorialInstance.eletitle : [UIFont fontWithName:@"Helvetica" size:13.0] :320 :UILineBreakModeWordWrap];  //Dyanamic label height

    totalHt = 171 - 21 + textHeight + 20;     // My calculation for text part of label
    return totalHt;

}

I did this for all the categories.

neha