2011-11-06 81 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会发生什么。 –