2011-11-06 121 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

相關問題