2

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.

Ken
  • 30,811
  • 34
  • 116
  • 155

1 Answers1

0

In this piece of code if you're just calculating the right line height and giving it to the table. So I suppose the cell height will be correct in both cases (iOS 4 and iOS 3). What happens is that you're not doing anything to adjust the two labels in the cell contentView, so probably the iOS 4 cell is auto-adjusting, while iOS 3 no. Now you may try to study the iOS 3 subtitle cell hierarchy and apply the right changes or you can make your own custom labels and add them to the contentView hierarchy: of course these custom labels will have their frame, number of lines, font, ... calculated in the same way you used in the line height code (so you should start refactoring your code by exporting the line height calculation in a common method).

viggio24
  • 12,316
  • 5
  • 41
  • 34