Trouble with Making UITableViewCells Dynamic

I have a subclassed UITableViewCell called Comment, which has a UILabel containing some relevant information. This information is of varying length, so it is merely impossible for me to set a appropriate height for the UILabel.

I have tried making the cell dynamic, but the UILabel either overflows the cell (y axis) or the cell is too big.

Please can you tell me where I am going wrong?!

tableView... cellForRowAtIndexPath...

[cell->commentText setLineBreakMode:UILineBreakModeWordWrap];
[cell->commentText setMinimumFontSize:14];
[cell->commentText setNumberOfLines:0];
[cell->commentText setFont:[UIFont systemFontOfSize:14]];

NSString *text = [cell->commentText text];

CGSize constraint = CGSizeMake(320, 20000.0f);

CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

[cell->commentText setText:text];
[cell->commentText setFrame:CGRectMake(20, 40, 280, MAX(size.height, 125))];


- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *text = [[[comments objectAtIndex:indexPath.row] body] stringByStrippingHTML];

    CGSize constraint = CGSizeMake(320, 20000.0f);

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

    CGFloat height = MAX(size.height, 125);

    return height + 20;
}

Answers


Are you stripping the html in your cellForRowAtIndexPath:also?

I've dynamically resized labels two different ways which have both worked:

1) In IB, anchor the label to the top and bottom of the cell and set its height to flexible (under the ruler icon in the right side panel). Then, just implement heightForRowAtIndexPath as you've done.

2) In cellForRowAtIndexPath, set the label's width and then call [label sizeToFit]. Use your same heightForRowAtIndexPath.


Need Your Help

Inner join a single row by getting only the highest integer row

mysql join inner-join

When I inner join the second table I only want to join a single row. The criteria is to take the row with the highest integer in the level column. I was thinking something like this:

Windows has generated a breakpoint

c++ visual-studio-2010 winapi

I'm debugging my C++ Win32 program in VS2010 and I always get "Windows has triggered a breakpoint in program.exe".