2013-01-21 45 views
2

我有一個正確的細節單元格佈局的分組tableview。現在我從我的服務器上取回數據。 有時候,我碰到了一個非常大的文本。所以tableviewCell應該比別人高。你可以在這裏看到我的問題的截圖。取決於文本的UITableViewCell的動態高度

enter image description here

就像你可以在我的第一個單元格中看到我有一個非常大的文本。現在我希望單元格的高度隨着文本的長度而增長,所以我總是可以看到整個文本而不是'...'。這就是我在代碼中的時刻。

#define PADDING 10.0f 
- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    Subscription *sub = [_dictTask valueForKeyPath:@"subscription"]; 

    NSArray *meta = sub.meta.allObjects; 

    NSString *text = [[meta objectAtIndex:indexPath.row]valueForKey:@"sum_value"]; 
    CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.tableView.frame.size.width - PADDING * 3, 1000.0f)]; 

    return textSize.height + PADDING * 3; 
} 

編輯 代碼,我的時刻。

- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    Subscription *sub = [_dictTask valueForKeyPath:@"subscription"]; 

    NSArray *meta = sub.meta.allObjects; 

    NSString *text = [[meta objectAtIndex:indexPath.row]valueForKey:@"sum_value"]; 
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0]; 
    CGSize constraintSize = CGSizeMake(330.0f, 400); // Make changes in width as per your label requirement. 

    CGSize textSize = [text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 

    return textSize.height; 
} 

這就是結果。

enter image description here

編輯的cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    Subscription *sub = [_dictTask valueForKeyPath:@"subscription"]; 
    NSArray *meta = sub.meta.allObjects; 
     static NSString *CellIdentifier = @"Cell"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 


     cell.textLabel.text = [[meta objectAtIndex:indexPath.row]valueForKey:@"sum_label"]; 
     cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap; 
     cell.detailTextLabel.numberOfLines = 0; 
     cell.detailTextLabel.text = [[meta objectAtIndex:indexPath.row]valueForKey:@"sum_value"]; 

     cell.textLabel.textColor = [UIColor colorWithRed:102/255.0 
               green:102/255.0 
               blue:102/255.0 
               alpha:1.0]; 
     cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18]; 
     return cell; 



} 
+0

什麼問題? – robhayward

+0

+1爲你的努力。儘管之前提出了很多類似的問題。 ;-) –

+0

'CGSizeMake(330.0f,400);'嘗試'MAXFLOAT'而不是400,並讓我知道會發生什麼。 –

回答

1

你需要在你的代碼中使用lineBreakMode:UILineBreakModeWordWrap

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *text = [[meta objectAtIndex:indexPath.row]valueForKey:@"sum_value"]; 
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0]; 
    CGSize constraintSize = CGSizeMake(330.0f, MAXFLOAT); // Make changes in width as per your label requirement. 

    CGSize textSize = [text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 

    return textSize.height; 
} 

而且需要設置detailTextLabel的兩個屬性also in datasource method cellForRowAtIndexPath`這樣的:

- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap; 
    cell.detailTextLabel.numberOfLines = 0; 
} 
  • numberofLines屬性描述:

    此屬性控制線的最大數量以便使標籤的文本適合其邊界矩形。此屬性的默認值是1。要刪除任何上限,並根據需要使用盡可能多的行,這個屬性的值設置爲0

+0

謝謝你的回答,但這不起作用。還需要返回textSize.height。因爲它考慮了CGFloat。 – Steaphann

+0

是的。錯過了。更改了代碼。在我的實現中,這個邏輯工作。 –

+0

@StefGeelen:用信息更新代碼。現在應該工作.. –

0

試試這個:

- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
      Subscription *sub = [_dictTask valueForKeyPath:@"subscription"]; 

      NSArray *meta = sub.meta.allObjects; 

      NSString *text = [[meta objectAtIndex:indexPath.row]valueForKey:@"sum_value"]; 
      UIFont *cellFont = [UIFont fontWithName:@"Helvetica-Bold" size:18.0]; 
      CGSize constraintSize = CGSizeMake(330.0f, MAXFLOAT); // Make changes in width as per your label requirement. 

      CGSize textSize = [text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 

    return textSize.height + 20; } 

然後請考慮使用唯一的單元格標識符這裏的想法是重複使用單元格會導致內容的高度不匹配。 (有些人不喜歡的唯一標識符的想法,但他們的工作,他們是沒有問題的,除非你的表將是巨大的),因此,試試這個:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    Subscription *sub = [_dictTask valueForKeyPath:@"subscription"]; 
    NSArray *meta = sub.meta.allObjects; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%i", indexPath.row]]; 

    if (cell == nil){ 
     cell.textLabel.text = [[meta objectAtIndex:indexPath.row]valueForKey:@"sum_label"]; 
     cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap; 
     cell.detailTextLabel.numberOfLines = 0; 
     cell.detailTextLabel.text = [[meta objectAtIndex:indexPath.row]valueForKey:@"sum_value"]; 

     cell.textLabel.textColor = [UIColor colorWithRed:102/255.0 
               green:102/255.0 
               blue:102/255.0 
               alpha:1.0]; 
     cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18]; 
} 
     return cell; 
} 
相關問題