2009-07-21 63 views
7

我有一個UITableView(分組!),需要計算兩種樣式的單元格的高度:UITableViewCellStyleDefaultUITableViewCellStyleValue2heightForRowAtIndexPath更長的NSStrings

這是我如何做到這一點的UITableViewCellStyleDefault

CGSize textSize = {300.f, 200000.0f}; 
CGSize size = [myTextString1 sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap]; 
size.height += 30.0f; 
result = MAX(size.height, 44.0f); 

而對於UITableViewCellStyleValue2

CGSize textSize = {207.f, 200000.0f}; 
CGSize size = [myTextString2 sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap]; 
size.height += 30.0f; 
result = MAX(size.height, 44.0f); 

我的問題是,他們返回不正確的高度,我認爲這是textSize哪裏我使用不正確的數字。對於長文本,底部部分被縮短(通常只是一個帶有幾個字的行),並且對於兩個CellStyles,它們在單元格中的文本之上和之下都有奇怪的間距。

對於UITableViewCellStyleValue2我得到的寬度大小(207.0f)使text.backgroundColor紅色,然後只是計算框的大小。 300.0fUITableViewCellStyleDefault的寬度與的單元格大小有關。

有誰知道哪些值我需要使用正確計算NSString的大小,從而得到適當的高度,我的電池?

感謝

回答

9

這是我爲此使用的代碼。它像一種細胞的魅力。它可能爲您的應用程序提供一些有用的部分。

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath { 
AppAppDelegate *appDelegate = (AppAppDelegate *)[[UIApplication sharedApplication] delegate]; 
NSString *Text = ([[appDelegate.myTextSectionsDelegateDict objectAtIndex:indexPath.section] objectForKey:@"Text"]); 

    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0]; 
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT); 
    CGSize labelSize = [Text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 
    return labelSize.height + 15;} 
+1

謝謝。我以同樣的方式獨自做了一件事。我發現我在使用Value2單元格時遇到問題,因爲我認爲它們是fontSize 14,實際上它們是15. 對於您的代碼,您可能需要添加: result = MAX(labelsize.height + 15 ,44.0f); 以防萬一,如果單元格是空的或少於2行,它會返回最小行高爲44.0f,這是正常的。如果只有一條線,它會使它小於44.0f。 – runmad 2009-07-27 19:40:03

2

當你創建你的,你使用相同的字體,你用它來衡量呢?

我用this page教程和一切爲我工作。它也許對你有用:

+0

是的,我使用相同的字體大小和類型(粗體/不粗體)。我會看看鏈接,看看是否解決它並讓你知道。 – runmad 2009-07-21 19:00:53

+0

我發現我沒有使用正確的字體!我發現我在使用Value2單元格時遇到問題,因爲我認爲它們是fontSize 14,而它們實際上是15.我需要使用其他單元格樣式來調整文本高度。 – runmad 2009-07-27 19:41:04

2

它看到你已經發現你使用了錯誤的文字大小。您可能只想使用標籤的fontlineBreakMode屬性以避免將來出現此問題,尤其是如果您稍後在單元格中更改它們時。另外,爲了便於閱讀,我會避免在返回數字前增加高度。相反,我會嘗試這樣的事:

CGSize textSize = CGSizeMake(300.0, 1979); 
CGSize size = [ myTextString1 sizeWithFont:[[ cell textLabel ] font ] 
         constrainedToSize:textSize 
          lineBreakMode:[[ cell textLabel ] lineBreakMode ]]; 

result = MAX(size.height + 30, 44.0f); 

我用1979年,因爲根據該文件,一個不應該返回值大於2009年

0

當爲我工作是基於計算constraintSize表視圖的寬度:

CGSize constraintSize = CGSizeMake(tableView.frame.size.width * 0.6, 2009); 
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 
0

最簡單的方法來做到這一點:

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 

CGFloat result = 10.0f; 
if(indexPath.section == 1) // here i compare the sections 
{ 
    result = 400.0f; 
} 
return result; 
} 

根據您擁有的部分返回值。這會給你自定義的行高。

2

如果你要計算的高度妥善爲cellvalue2你應該使用: [UIFont boldSystemFontOfSize:15.0f]和linebreakmode:NSLineBreakByTruncatingTail

如果不使用粗體,文本將正確填寫,但你會失去正確的垂直邊距。

其餘的計算是正確的。