2011-11-06 154 views
0

我从异步json服务器获取数据,我将它们放在我的表格视图单元格中。我试图根据文本内容在动态基础上制作单元格高度。我发现这件事互联网上却处处其给出了相同的代码,使用 -动态uitableview单元格高度

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

我没有得到如何实现this.Please指导我。

回答

0

在此方法中,计算图像的高度,每行中使用的文本并将高度作为浮点值返回。

对于如:

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

float totalHeight = 0.0; 

BOOL ImageToBeDisplayed = [[yourDataArray objectAtIndex:indexPath.row][email protected]"yourImageKey"]; 

if(ImageToBeDisplayed) 

totalHeight = 100 or some value of your choice 
} 
else{ 
//Do nothing 
} 

If your row has string data as well. 

int length = [[[yourDataArray objectAtIndex:indexPath.row][email protected]"yourTextKey"] length]; 
if(length<40) 
totalHeight + = 50; 
else if (length<80 && length>40) 
totalHeight + = 100; 
and so on 

and 


return totalHeight; 

} 
+0

感谢琼斯:-) – codejunkie

+0

为什么有硬编码常数?如果字体大小发生变化怎么办? – mskw

相关问题