2011-11-06 80 views
1

我可以在表格中顯示單元格內容,也可以Wordwrap它來完全顯示消息。但我注意到,如果內容比高度多,則會變得很團結。如果內容太大,如何增加動態提高單元格的大小。由於我從url中檢索數據,所以長度會有所不同,所以我不想對高度進行硬編碼,但希望根據內容長度對其進行更改。動態調整tableView單元格的高度

,我已經試過到目前爲止是爲下的代碼:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSDictionary *person = [myPeople objectAtIndex:[indexPath row]]; 

    NSString *personName = [person valueForKey:@"text"]; 

    NSString *cellText = personName; 
    UIFont *cellFont  = [UIFont fontWithName:@"Helvetica-neuve" size:21.0]; 
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT); 
    CGSize labelSize  = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 
    int buffer = 70; 
    return labelSize.height + buffer; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [commentView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; 
     cell.textLabel.numberOfLines = 6; 
     cell.textLabel.font   = [UIFont fontWithName:@"Helvetica-neuve" size:21.0]; 
     [cell.textLabel setMinimumFontSize:13.0]; 
     [cell.textLabel setAdjustsFontSizeToFitWidth:NO]; 
    } 
    NSDictionary *person = [myPeople objectAtIndex:[indexPath row]]; 

    NSString *personName = [person valueForKey:@"text"]; 
    cell.textLabel.text = personName; 
    return cell; 
    } 

回答

1

雖然我不喜歡張貼只是一個鏈接。這將告訴你如何爲你的細胞在飛行中計算正確的高度。

Adjust UILabel height depending on the text

+0

但我想調整單元格高度 – iDev

+0

對不起,我錯了。我看到你已經在使用這種方法。當你用相同的方法鉤住willDrawCell會發生什麼。 –