dynamic uitableview cell height

2019-03-03 13:10发布

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条回答
Evening l夕情丶
2楼-- · 2019-03-03 13:57

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;

}
查看更多
登录 后发表回答