dynamic uitableview cell height

大城市里の小女人 提交于 2019-12-25 02:01:38

问题


I am getting data from json server asynchronously and I am putting them in my table view cells.I am trying to make the cell height on dynamic basis depending upon the text content. I have found about it on internet but everywhere its given the same code , using -

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

I am not getting how to implement this.Please guide me.


回答1:


In this method, calculate the height of images, text being used in each row and return the height as a float value.

For eg:

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

float totalHeight = 0.0;

BOOL ImageToBeDisplayed = [[yourDataArray objectAtIndex:indexPath.row]objectForKey@"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]objectForKey@"yourTextKey"] length];
if(length<40)
totalHeight + = 50;
else if (length<80 && length>40)
totalHeight + = 100;
and so on

and


return totalHeight;

}


来源:https://stackoverflow.com/questions/8030533/dynamic-uitableview-cell-height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!