I have a table displaying different texts, therefore I need to adjust my cell height accordingly. I use the delegate method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
// I ask for my content string and then I want to adjust the cell accordingly:
CGSize maximumSize = CGSizeMake(defaultCell.textLabel.frame.size.width, CGFLOAT_MAX);
GSize neededSize = [contentString sizeWithFont:defaultCell.textLabel.font constrainedToSize:maximumSize lineBreakMode:defaultCell.textLabel.lineBreakMode];
CGFloat cellHeight = neededSize.height +15;
return cellHeight;
The default cell is my protoype cell of the storyboard. I initialise it in the viewDidAppear delegate method so I can access easily my protoype uitableviewcell, in this way I only have to change something like the font in my storyboard.
The Problem I have is that the function sizeWithFont: constrainedToSize: lineBreakMode: does not give me accurate answers. If there are only a few very short words in the next line it will give a result one line too low.
Does anybode have an idea why that is and how to avoid it?
There can be differences in the cell width when you load the view, and when it's rendered. My shot-in-the-dark would be that the cell frame you think you're working with, isn't the same cell frame that's being rendered.