在iOS4和iOS3中调整UITableViewCells的大小 [英] Resizing UITableViewCells in iOS4 versus iOS3

查看:50
本文介绍了在iOS4和iOS3中调整UITableViewCells的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码来调整UITableViewCellStyleSubtitle单元格的大小,以适合可以运行多行的文本:

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;
}

这在iOS4.*中运行良好,并提供了容纳文本的单元格.(如果有的话,它们太大了,但是没关系.)

This runs fine in iOS4.* and gives cells that accommodate the text. (If anything they're slightly too big but that's OK.)

但是当我在iOS3.*中尝试此操作时,该单元格被弄乱了:textLabel出现在单元格中的低处,然后detailLabel文本通常溢出到下一个单元格中或被截断为"...".textLabel似乎出现在单元格的中间,而不是顶部.

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.

iOS3和iOS4之间可能有什么不同?我该怎么办才能解决这个问题?或者,是否有可以很好地替代上述(例如)在两个iOS上都可以使用的方法.

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.

提前谢谢您.

推荐答案

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

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).

这篇关于在iOS4和iOS3中调整UITableViewCells的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆