views:

94

answers:

1

When dealing with UITableViewControllers, is there a way to set the height of an individual table cell other than this method:

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

Basically what I am trying to do is insert a block of text into a table cell that would have a variable height. The text could be anywhere between 100-1000 characters long. What I would like to do is make the table cell's height fit the block of text that will be inserted into it. What I am NOT looking is a way to shrink the text to fit into a table cell row of a fixed height (which seems to be the solutions I am finding). Is there a way to call the cell's height through the following method?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

Currently, I am doing the following snippet of code to find the height of the text however I don't feel this is the correct / best way of doing this. If I were get the size constraints of the block of text then it only seems logical that I could update the table cell with the proper heights through this method: cellForRowAtIndexPath. Also I was reading that the values returned below are in pixels so would I need to convert from pixels to some other numerical value that would give me the correct height for the row?

CGSize constraintSize;
constraintSize.width = 500.0f;
constraintSize.height = 500.0f;
CGSize theSize = [theText sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

UPDATE: So this link seems to do what I wanted: http://stackoverflow.com/questions/1352801/get-tableviewheightforrowatindexpath-to-happen-after-tableviewcellforrowatinde

However, the above link still seems to clip the last few characters / words of the text. It all seems to depend on the number of characters / words used in the block of text. I could be wrong. Any ideas?

A: 

The last characters being clipped could be caused by a too wide constrain size. Reduce it by a few pixels and see how it works out. You might also add a constant offset on the returned height. All UILabels have a small margin that might not be taken into account correctly in your referenced answer.

Max Seelemann