I'm using the following code to size a UITableViewCellStyleSubtitle cell to fit text that can run to more than one line:
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGRect frame = [UIScreen mainScreen].bounds;
CGFloat width = frame.size.width;
int section = indexPath.section;
int row = indexPath.row;
NSString *title_string = [[my_array_ objectAtIndex:row]
valueForKey:@"keyOne"];
NSString *detail_string = [[my_array_ objectAtIndex:row]
valueForKey:@"keyTwo"];
CGSize title_size = {0, 0};
CGSize detail_size = {0, 0};
if (title_string && ![title_string isEqualToString:@""]) {
title_size = [title_string sizeWithFont:
[UIFont systemFontOfSize:TITLE_FONT_SIZE]
constrainedToSize:CGSizeMake(width, 4000)
lineBreakMode:UILineBreakModeWordWrap];
}
if (detail_string && ![detail_string isEqualToString:@""]) {
detail_size = [detail_string sizeWithFont:
[UIFont systemFontOfSize:DETAIL_FONT_SIZE]
constrainedToSize:CGSizeMake(width, 4000)
lineBreakMode:UILineBreakModeWordWrap];
}
CGFloat title_height = title_size.height;
CGFloat detail_height = detail_size.height;
CGFloat content_size = title_height + detail_height;
CGFloat height;
switch (section) {
case 0:
height = content_size; //+ offset;
break;
default:
height = 44.0;
break;
}
return height;
}
This runs fine in iOS4.* and gives cells that accommodate the text. (If anything they're slightly too big but that's OK.)
But when I try this in iOS3.* the cell is messed up: The textLabel appears low down in the cell and the detailLabel text often then spills over into the next cell or is truncated with "...". The textLabel seems to appear in the middle of the cell, rather than at the top.
What is different between iOS3 and iOS4 that might cause this? What can I do to resolve this? Or, is there a good alternative to the above (say) that will work on both iOSes.
Thanking you kindly in advance.