2011-02-21 42 views

回答

1

當然,

我喜歡它傳遞模型(只是一個NSObject的數據),則它設置在我的手機中的所有數據。然後我創建一個自定義單元格,可以通過模型設置其數據,並添加一個類級別函數以返回大小。它看起來像這樣。

TableViewDelegate:

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    XTableViewCellModel *model = [self modelForIndexPath:indexPath]; 
    return [XTableViewCell cellHeight:model]; 
} 

XTableViewCell:

+(CGFloat)cellHeight:(XTableViewCellModel*)model { 
    CGSize titleSize = [model.title sizeWithFont:model.titleFont 
         constrainedToSize:CGSizeMake(280, 9999) 
          lineBreakMode:UILineBreakModeTailTruncation]; 
    return titleSize.height; 
} 

請注意,設置lineBreakMode到UILineBreakModelTailTruncation只需要9999點的高度達到後生效。在此之前,文字通常會包裝。

看看我的圍繞這個東西的開源框架。它有很多默認建立的可調整大小的細胞類型:https://github.com/andrewzimmer906/XCell