2011-11-30 27 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