2010-09-08 68 views
2

我想知道自適應細胞高度的最佳策略。這些單元格首先知道在cellForRowAtIndexPath方法中它們創建時的高度(它們包含一些文本框和圖像)。 我的想法是將單元格的高度存儲在帶有單元標識鍵的NSMutableDictionary中。 問題是,heightForRowAtIndexPath:方法被稱爲之前單元格創建,然後才。UITableView:自適應細胞高度

你是如何管理的?

回答

6

如果你要調整你的細胞(即 - 使heightForRowAtIndexPath方法被調用),可以使用空beginUpdates - endUpdates塊:

[table beginUpdates]; 
[table endUpdates]; 

這將迫使UITableView的更新它的幾何形狀和細胞的高度會更新

0

如果您想調整您的單元格高度,請使用UITableViewDelegate中的 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath。 例如,將單元格的高度存儲到一個NSArray中,並獲取它得益於indexPath參數。

0

我現在以下:我從字符串高度直接計算的高度,我用:

-(CGFloat) estimateCellheightFromText:(NSString*) text andFont:(UIFont*)ffont widthConstraint:(CGFloat)width andLineBreakMode:(UILineBreakMode)lbMode{ 
CGSize stringSize = [text sizeWithFont:ffont constrainedToSize:CGSizeMake(width, 9999) lineBreakMode:lbMode]; return stringSize.height;}