heightForRowAtIndexPath用于更长的NSStrings [英] heightForRowAtIndexPath for longer NSStrings

查看:83
本文介绍了heightForRowAtIndexPath用于更长的NSStrings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableView (Grouped!)并且需要计算两种样式的单元格的高度: UITableViewCellStyleDefault UITableViewCellStyleValue2

I have a UITableView (Grouped!) and need to calculate the height of two styles of cells: UITableViewCellStyleDefault and UITableViewCellStyleValue2.

这就是我为 UITableViewCellStyleDefault做的事情

This is how I do it for UITableViewCellStyleDefault:

CGSize  textSize = {300.f, 200000.0f};
CGSize  size = [myTextString1 sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
size.height += 30.0f;
result = MAX(size.height, 44.0f);

对于 UITableViewCellStyleValue2

And for UITableViewCellStyleValue2:

CGSize  textSize = {207.f, 200000.0f};
CGSize  size = [myTextString2 sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
size.height += 30.0f;
result = MAX(size.height, 44.0f);

我的问题是他们返回了不正确的高度,我认为这是 textSize 我使用的数字不正确。对于长文本,底部部分会缩短(通常只有几个单词的行),对于两个CellStyles,它们在单元格中的文本上方和下方都有奇怪的间距。

My issue it that they return incorrect heights and I think it's the textSize where I use incorrect numbers. With long texts, the bottom part gets cut short (usually just a line with a few words), and for both CellStyles they have weird spacing above and below the text in the cell.

对于 UITableViewCellStyleValue2 我将text.backgroundColor设为红色然后只是宽度大小( 207.0f )计算盒子的大小。 300.0f UITableViewCellStyleDefault 的宽度是单元格的大小 UITableViewStyleGrouped

For UITableViewCellStyleValue2 I got the width size (207.0f) from making the text.backgroundColor red and then just calculating the size of the box. 300.0f width for UITableViewCellStyleDefault is how big the cell is with UITableViewStyleGrouped.

有谁知道我需要使用哪些值来正确计算 NSString 的大小,从而为我的细胞获得合适的身高?

Does anyone know which values I need to use to properly calculate the size of an NSString and thereby get appropriate height for my cells?

谢谢

推荐答案

这是我正在使用的代码。它就像一种细胞的魅力。它可能为您的应用程序提供了一些有用的部分。

Here is the code I am using for this. It works like a charm for one type of cell. It may have some useful parts for your application.

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath {
AppAppDelegate *appDelegate = (AppAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *Text = ([[appDelegate.myTextSectionsDelegateDict objectAtIndex:indexPath.section] objectForKey:@"Text"]);

    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
    CGSize labelSize = [Text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
    return labelSize.height + 15;}

这篇关于heightForRowAtIndexPath用于更长的NSStrings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆