动态uitableview单元格高度 [英] dynamic uitableview cell height
本文介绍了动态uitableview单元格高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从json服务器异步获取数据,我将它们放在我的表视图单元格中。我试图根据文本内容动态地设置单元格高度。我在互联网上找到了它,但在给定相同代码的地方,使用
-
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.
推荐答案
在此方法中,计算图像的高度,每行中使用的文本并将高度返回为一个浮动值。
In this method, calculate the height of images, text being used in each row and return the height as a float value.
例如:
-(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;
}
这篇关于动态uitableview单元格高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文