简体   繁体   English

在iOS4和iOS3中调整UITableViewCells的大小

[英]Resizing UITableViewCells in iOS4 versus iOS3

I'm using the following code to size a UITableViewCellStyleSubtitle cell to fit text that can run to more than one line: 我正在使用以下代码来调整UITableViewCellStyleSubtitle单元格的大小,以适合可以运行多行的文本:

- (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. 这在iOS4。*中运行良好,并提供了容纳文本的单元格。 (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 "...". 但是,当我在iOS3。*中尝试此操作时,该单元格被弄乱了:textLabel出现在单元格中的低处,然后detailLabel文本通常溢出到下一个单元格中或被截断为“ ...”。 The textLabel seems to appear in the middle of the cell, rather than at the top. textLabel似乎出现在单元格的中间,而不是顶部。

What is different between iOS3 and iOS4 that might cause this? iOS3和iOS4之间可能有什么不同? What can I do to resolve this? 我该怎么解决? Or, is there a good alternative to the above (say) that will work on both iOSes. 或者,是否有可以很好地替代上述(例如)在两个iOS上都可以使用的方法。

Thanking you kindly in advance. 预先感谢您。

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). 所以我想在两种情况下(iOS 4和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. 发生的事情是您没有做任何调整单元格contentView中的两个标签的操作,因此iOS 4单元格可能正在自动调整,而iOS 3则没有。 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). 现在,您可以尝试研究iOS 3字幕单元的层次结构并应用正确的更改,或者可以制作自己的自定义标签并将其添加到contentView层次结构:当然,这些自定义标签将具有其框架,行数,字体,。 ..以与在行高代码中使用的方式相同的方式进行计算(因此,应通过以通用方法导出行高计算来开始重构代码)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM