如何在iOS中动态处理tableviewCell的高度?
How to handle the height of tableviewCell dynamically in iOS?
这就是我现在做的!我是 iOS 开发的新手。
我想做的是像Facebook一样的newsfeed(cardViewUI),但是根据我对cell的设计,当文本长度超过label(cell的文本容器)的容量时,它会像你一样显示'..'见上文。
所以我想增加由文本长度动态设置的单元格高度。
我是用mainstoryboard设计的,不是.xib文件。我尝试 [label sizeToFit] 并将标签的行设置为 0,但到目前为止没有任何效果。
您可以按如下方式计算文字高度,
//Height For Row at index path
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText = [NSString stringWithFormat:@"%@",[arrFullChat[indexPath.row]objectAtIndex:1]];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:15.0];
NSAttributedString *attributedText =
[[NSAttributedString alloc]
initWithString:cellText
attributes:@
{
NSFontAttributeName: cellFont
}];
CGRect rect = [attributedText boundingRectWithSize:CGSizeMake(tableView.bounds.size.width - 90.0, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
return rect.size.height + 42;
}
此代码中的 42 将因您而异。尝试不同的值。
不要忘记使用自动布局或您想要的任何其他方式根据 Cell
高度增加您的 UITextView
高度。
使用 tableView:heightForRowAtIndexPath:
你可以在 运行 时给出每一行的大小。现在你的问题是如何从你的字符串中获取高度在 NSString class 中有函数通过这段代码你的问题,
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *str = [dataSourceArray objectAtIndex:indexPath.row];
CGSize size = [str sizeWithFont:[UIFont fontWithName:@"Helvetica" size:17] constrainedToSize:CGSizeMake(280, 999) lineBreakMode:NSLineBreakByWordWrapping];
NSLog(@"%f",size.height);
return size.height + 10;
}
在下面一行设置标签s num. of line to max. so set it in
cellForRowAtIndexPath:` 方法。
cell.textLabel.numberOfLines = 0;
if you use some custom cell then manage all label`s string with this
and get sum of all that height then set the height of your cell.
这就是我现在做的!我是 iOS 开发的新手。
我想做的是像Facebook一样的newsfeed(cardViewUI),但是根据我对cell的设计,当文本长度超过label(cell的文本容器)的容量时,它会像你一样显示'..'见上文。
所以我想增加由文本长度动态设置的单元格高度。
我是用mainstoryboard设计的,不是.xib文件。我尝试 [label sizeToFit] 并将标签的行设置为 0,但到目前为止没有任何效果。
您可以按如下方式计算文字高度,
//Height For Row at index path
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText = [NSString stringWithFormat:@"%@",[arrFullChat[indexPath.row]objectAtIndex:1]];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:15.0];
NSAttributedString *attributedText =
[[NSAttributedString alloc]
initWithString:cellText
attributes:@
{
NSFontAttributeName: cellFont
}];
CGRect rect = [attributedText boundingRectWithSize:CGSizeMake(tableView.bounds.size.width - 90.0, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
return rect.size.height + 42;
}
此代码中的 42 将因您而异。尝试不同的值。
不要忘记使用自动布局或您想要的任何其他方式根据 Cell
高度增加您的 UITextView
高度。
使用 tableView:heightForRowAtIndexPath:
你可以在 运行 时给出每一行的大小。现在你的问题是如何从你的字符串中获取高度在 NSString class 中有函数通过这段代码你的问题,
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *str = [dataSourceArray objectAtIndex:indexPath.row];
CGSize size = [str sizeWithFont:[UIFont fontWithName:@"Helvetica" size:17] constrainedToSize:CGSizeMake(280, 999) lineBreakMode:NSLineBreakByWordWrapping];
NSLog(@"%f",size.height);
return size.height + 10;
}
在下面一行设置标签s num. of line to max. so set it in
cellForRowAtIndexPath:` 方法。
cell.textLabel.numberOfLines = 0;
if you use some custom cell then manage all label`s string with this and get sum of all that height then set the height of your cell.