2012-01-30 67 views
0

这可能是一个简单的问题,但我希望通过正确的路径。在iphone中增加关于来自webservice的内容的单元格大小

从Web服务中获取一些内容并在表格视图中显示它。

从那里读取的文本的长度可能会有所不同。

有时它可能只有一个行句子,有时一个段落等

所以我怀疑是我如何可以根据从Web服务来的内容有所不同自定义单元格的高度。

希望得到您的帮助。

在此先感谢。

+0

同样的问题是这样的:-http://stackoverflow.com/questions/1443335/how-can-i-do-variable-height-table-cells-on-the-iphone-properly – Leena 2012-01-30 09:21:50

回答

0

让我们假设你存储阵列中的每个内容的文本。索引与表格单元格一致。 这是您要使用的代码。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
NSString *string = [array objectAtIndex:indexPath.row]; 

//300 is the width, you can change this to make it look right. 
CGSize maximumLabelSize = CGSizeMake(300 ,9999); 
CGSize expectedLabelSize = [string sizeWithFont:YOURLABEL.font 
            constrainedToSize:maximumLabelSize 
             lineBreakMode: UILineBreakModeWordWrap]; 

return expectedLabelSize.height; 

}

几乎涵盖基础。问问你是否还有其他问题。并阅读Leena的评论。它很有特色。

0

在你的UITableViewDelegate实现以下方法

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

根据行返回适当的高度。

您可以使用NSString的方法-sizeWithFont:来获取您获得的文本的像素大小。

http://developer.apple.com/library/IOs/documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html#//apple_ref/occ/instm/NSString/sizeWithFont

+0

但如何返回使用不同文字长度的不同高度 – suji 2012-01-30 09:17:24

+0

我已编辑我的答案并添加了计算文字大小的原因 – 2012-01-30 09:20:26

相关问题