2013-05-31 30 views
3

我有一個UITableView具有自定義的UITableViewCells,它們是來自用戶的評論。現在,我的單元格高達115.0f,但我希望根據評論的時間長度來改變高度。如果評論超過三行,我希望用戶能夠選擇單元格,單元格可以展開以顯示整個評論。我一直在使用[UIView animateWithDuration: completion:方法來展開單元格,但我不知道如何根據文本的長度計算出單元格的正確尺寸。有人可以幫我嗎?下面是一些代碼:如何根據NSString計算動態大小的UITableViewCells所需高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    if (indexPath.row == 0) 
    { 
     return 480; 
    } 
    else 
    { 
     if (self.cellType == 0) 
     { 
      return 115; 
     } 
     else 
     { 
      return 75; 
     } 
    } 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row > 0) 
    { 
     NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]; 
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:path]; 
     if ([cell isKindOfClass:[CommentCell class]]) 
     { 
      CommentCell *cell = (CommentCell *)[tableView cellForRowAtIndexPath:indexPath]; 
      UILabel *label = cell.commentBodyLabel; 
      NSString *theText = label.text; 
      CGSize constraintSize = CGSizeMake(label.frame.size.width, label.frame.size.height); 
      CGSize labelSize = [theText sizeWithFont:label.font constrainedToSize:constraintSize lineBreakMode:label.lineBreakMode]; 
      CGFloat labelHeight = labelSize.height; 
      int numLines = (int)(labelHeight/label.font.leading); 
      NSLog(@"there are %i lines", numLines); 
      NSLog(@"The text height is %f", labelHeight); 
      if (numLines == 3) 
      { 
       //This is where I should expand the cell 
      } 
     } 

回答

1

看看到NSString UIKit Additions

您在sizeWithFont:constrainedToSize:lineBreakMode:

設置constrainedToSize財產CGSize.width到你的/標籤區域的寬度特別感興趣。然後將CGSize.height設置爲非常大的數字,可能是CGFLOAT_MAX。這個想法是,你在說:「嘿,這個標籤必須適合一個寬度爲靜態的區域,但它可以永久垂直放置,所以,告訴我這個標籤實際上會給予我給你的信息有多高「。

NSString *comment = @"Some really long comment that does not fit in the standard cell size. This comment will be wrapped by word. Some more words to make this longer..."; 

CGSize renderedSize = [comment sizeWithFont:myFont constrainedToSize:CGSizeMake(kCellWidth, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping]; 

renderedSize.height是你將返回(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

+1

感謝您的快速響應! –

+1

該方法爲renderedSize返回0。 –

1

一種方法是instantiate prototype cells,讓他們計算基於給定的數據其動態高度的價值。這種方法可以讓你依賴故事板佈局(如果你使用故事板),而不必在視圖控制器中硬編碼你的單元配置的知識。

編輯我使用TLIndexPathTools,如果你的電池實現TLDynamicHeightView協議,它會自動計算出動態的高度爲你添加了動力高度標記細胞的工作示例。請參閱"Dynamic Height" example project。高度計算在小區做過這樣的:

@interface DynamicHeightCell() 
@property (nonatomic) CGSize originalSize; 
@property (nonatomic) CGSize originalLabelSize; 
@end 

@implementation DynamicHeightCell 

- (void)awakeFromNib 
{ 
    [super awakeFromNib]; 
    self.originalSize = self.bounds.size; 
    self.originalLabelSize = self.label.bounds.size; 
} 

- (void)configureWithText:(NSString *)text 
{ 
    self.label.text = text; 
    [self.label sizeToFit]; 
} 

#pragma mark - TLDynamicSizeView 

- (CGSize)sizeWithData:(id)data 
{ 
    [self configureWithText:data]; 
    CGSize labelSize = self.label.bounds.size; 
    CGSize size = self.originalSize; 
    size.width += labelSize.width - self.originalLabelSize.width; 
    size.height += labelSize.height - self.originalLabelSize.height; 
    return size; 
} 

@end 

從本質上說,你還記得原來的大小時,將小區從故事板喚醒。然後在執行計算時,請在標籤上呼叫sizeToFit,並使用新尺寸計算高度增量並將其添加到原始高度。在故事板中,您需要將標籤的寬度設置爲所需的寬度,並將數字行設置爲0.

在視圖控制器方面,如果您使用的是TLIndexPathTools,則只需設置數據模型與字符串數組並設置單元格上的標籤。原型和尺寸計算由基類TLTableViewController類自動完成。如果你不想使用TLIndexPathTools,那麼從TLTableViewController中提取位應該可以讓你繼續。

@implementation DynamicHeightTableViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.indexPathController.items = @[ 
      @"Lorem ipsum dolor sit amet, consectetur adipiscing elit.", 
      @"Fusce ac erat at lectus pulvinar porttitor vitae in mauris. Nam non eleifend tortor.", 
      @"Quisque tincidunt rhoncus pellentesque.", 
      @"Duis mauris nunc, fringilla nec elementum nec, lacinia at turpis. Duis mauris nunc, fringilla nec elementum nec, lacinia at turpis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.", 
      ]; 
} 

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *item = [self.indexPathController.dataModel itemAtIndexPath:indexPath]; 
    DynamicHeightCell *dynamicCell = (DynamicHeightCell *)cell; 
    [dynamicCell configureWithText:item]; 
} 
+0

最終他的問題是「但我不知道如何根據文本的長度計算出單元的正確大小。」 –

+0

增加了一個工作示例。希望有所幫助。 –

+0

@ChrisWagner對不起,我正在組織一個工作示例來說明解決方案。 –

相關問題