Iphone 如何根据文本动态定义uitableviewcell高度
我正在使用分组uitableview。我有很多章节。 在第一部分中,只有一个UITableViewCell。在这个单元格中,我将显示一个文本 问题是我不知道文本的字符数,我想更改单元格的高度以显示整个文本 有人能帮我吗?Iphone 如何根据文本动态定义uitableviewcell高度,iphone,objective-c,uitableview,Iphone,Objective C,Uitableview,我正在使用分组uitableview。我有很多章节。 在第一部分中,只有一个UITableViewCell。在这个单元格中,我将显示一个文本 问题是我不知道文本的字符数,我想更改单元格的高度以显示整个文本 有人能帮我吗? 感谢使用UItableViewDelegate方法-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(nsindepath*)indepath可以返回特定行的高度。为此,您必须知道要在该行中显
感谢使用UItableViewDelegate方法-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(nsindepath*)indepath可以返回特定行的高度。为此,您必须知道要在该行中显示的字符串的长度。
查看此链接您将不得不使用
高度作为行路径
。在此范围内,您将必须执行在单元格的布局子视图
或单元格的行索引路径
中执行的所有计算。为此,您必须计算文本的长度。无论如何,您都必须访问cellForRowAtIindexPath
中的文本。那你为什么不知道这段文字会有多长呢
要确定实际文本的大小,可以使用NSSTring的
sizeWithFont:forWidth:lineBreakMode:
我解决了应用下面的代码并创建属性numberOfTextRows的问题
(CGFloat)tableView:(UITableView*)t rowatindexpath的高度:(nsindepath*)indepath
float height = 45.0f;
if (indexPath.section == 0 && indexPath.row == 0) {
CGSize theSize = [self.strResult sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:CGSizeMake(265.0f, 9999.0f) lineBreakMode:UILineBreakModeWordWrap];
self.numberOfTextRows = round(theSize.height / 12);
if ((indexPath.section == height) || (self.numberOfTextRows < 2)) {
height = 45;
} else {
height = theSize.height + 16;
}
}
return height;
浮动高度=45.0f;
if(indexPath.section==0&&indexPath.row==0){
CGSize theSize=[self.strResult sizeWithFont:[UIFont systemFontOfSize:12.0f]约束的大小:CGSizeMake(265.0f,9999.0f)lineBreakMode:UILineBreakModeWordWrap];
self.numberOfTextRows=圆形(size.height/12);
if((indexPath.section==高度)| |(self.numberOfTextRows<2)){
高度=45;
}否则{
高度=尺寸高度+16;
}
}
返回高度;
(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
float height = 45.0f;
if (indexPath.section == 0 && indexPath.row == 0) {
CGSize theSize = [self.strResult sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:CGSizeMake(265.0f, 9999.0f) lineBreakMode:UILineBreakModeWordWrap];
self.numberOfTextRows = round(theSize.height / 12);
if ((indexPath.section == height) || (self.numberOfTextRows < 2)) {
height = 45;
} else {
height = theSize.height + 16;
}
}
return height;
cell.textLabel.lineBreakMode=UILineBreakModeWordWrap
cell.textlab.numberOfLines=self.numberOfTextRows
您必须知道文本的数量或如何在单元格中显示文本?此外,请查看与您的问题相关的所有链接。这张是20分钟前的