2011-11-30 51 views
0

我有一个名为Comment的子类UITableViewCell,它有一个包含一些相关信息的UILabel。这些信息的长度各不相同,因此我只能为UILabel设置合适的高度。使UITableViewCells动态的问题

我已经尝试使单元格动态,但UILabel要么溢出单元格(y轴)或单元格太大。

请你能告诉我哪里出错了吗?

tableView... cellForRowAtIndexPath... 

[cell->commentText setLineBreakMode:UILineBreakModeWordWrap]; 
[cell->commentText setMinimumFontSize:14]; 
[cell->commentText setNumberOfLines:0]; 
[cell->commentText setFont:[UIFont systemFontOfSize:14]]; 

NSString *text = [cell->commentText text]; 

CGSize constraint = CGSizeMake(320, 20000.0f); 

CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

[cell->commentText setText:text]; 
[cell->commentText setFrame:CGRectMake(20, 40, 280, MAX(size.height, 125))]; 


- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *text = [[[comments objectAtIndex:indexPath.row] body] stringByStrippingHTML]; 

    CGSize constraint = CGSizeMake(320, 20000.0f); 

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

    CGFloat height = MAX(size.height, 125); 

    return height + 20; 
} 

回答

0

您是否在剥离您的cellForRowAtIndexPath:中的html?

我动态调整大小的标签,其都设法两种不同的方式:

1)在IB,锚标签到电池的顶部和底部,其高度设置为灵活(在标尺图标下右侧面板)。然后,就像你所做的那样实施heightForRowAtIndexPath

2)在cellForRowAtIndexPath中,设置标签的宽度,然后调用[label sizeToFit]。使用相同的heightForRowAtIndexPath