iPhone:如何根据字符串动态增加单元格的高度 [英] iPhone: How to increase the height of cell dynamically as per the string
问题描述
我在我的应用程序中使用带有标题的UITableView,我想在tableview的单元格中添加字符串.在标题下面的单元格中,我想填充字符串数据,并根据字符串的大小,我要增加单元格的高度
I am using UITableView with header in my app and I want to add string in the cell of tableview. In the cell below the header I want to fill string data and as per the size of the string I want to increase the height of the cell
为了更好地理解,我附加了文件.
for better understanding I attach the file.
但是我无法获得想要的答案.
but I am not able to get answer which I want.
为了更好地理解,我也附上了图片.
for better understanding I attach the image too.
标签说明指示我的标题,并在下一个单元格中添加字符串.
label Instructions indicates my header and in next cell I want to add string.
然后如何根据字符串长度增加单元格的高度.
then How can I increase the height of cell according to string length.
先谢谢.
推荐答案
实现方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//return appropriate value based on IndexPath.row
UIFont *font = textLabel.font; // use custom label's font
NSString *myText = @"your text for row at index path";
CGSize size = [myText sizeWithFont:font forWidth:textLabel.frame.size.width lineBreakMode:UILineBreakModeWordWrap];
return (size.height+20); //10 pixels space above the textlabel and below 10 pixels space
}
如您所引用的链接中所述.
as mentioned in the link you have referred.
这篇关于iPhone:如何根据字符串动态增加单元格的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!