簡體   English   中英

在iOS4和iOS3中調整UITableViewCells的大小

[英]Resizing UITableViewCells in iOS4 versus iOS3

我正在使用以下代碼來調整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;
}

這在iOS4。*中運行良好,並提供了容納文本的單元格。 (如果有的話它們太大了,但是沒關系。)

但是,當我在iOS3。*中嘗試此操作時,該單元格被弄亂了:textLabel出現在單元格中的低處,然后detailLabel文本通常溢出到下一個單元格中或被截斷為“ ...”。 textLabel似乎出現在單元格的中間,而不是頂部。

iOS3和iOS4之間可能有什么不同? 我該怎么解決? 或者,是否有可以很好地替代上述(例如)在兩個iOS上都可以使用的方法。

預先感謝您。

在這段代碼中,如果您只是在計算正確的行高並將其提供給表格。 所以我想在兩種情況下(iOS 4和iOS 3)單元格高度都是正確的。 發生的事情是您沒有做任何調整單元格contentView中的兩個標簽的操作,因此iOS 4單元格可能正在自動調整,而iOS 3則沒有。 現在,您可以嘗試研究iOS 3字幕單元的層次結構並應用正確的更改,或者可以制作自己的自定義標簽並將其添加到contentView層次結構:當然,這些自定義標簽將具有其框架,行數,字體,。 ..以與在行高代碼中使用的方式相同的方式進行計算(因此,應通過以通用方法導出行高計算來開始重構代碼)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM