2011-02-06 36 views
2

我用下面的代碼大小UITableViewCellStyleSubtitle細胞,使其適用於可以運行一個以上的一行文字:調整大小UITableViewCells中的iOS4對IOS3

- (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應用程序的好方法(比如說)。

感謝您提前。

回答

0

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

+0

感謝您的帖子。在這個階段,它看起來更容易去定製我自己的細胞。 – SK9 2011-02-06 13:16:46