heightForRowAtIndexPath for longer NSStrings

2020-02-26 00:29发布

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

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);

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);

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.

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.

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?

Thanks

6条回答
唯我独甜
2楼-- · 2020-02-26 00:52

Easiest way to do this:

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

CGFloat result = 10.0f;
if(indexPath.section == 1) // here i compare the sections
{
    result = 400.0f;
}
return result;
}

Return the values based on the sections you have. This will give you custom row heights.

查看更多
叛逆
3楼-- · 2020-02-26 00:53

When you create your cell, are you using the same font as the one you use to measure?

I used the tutorial on this page and everything worked for me. It might be useful to you too:

查看更多
Emotional °昔
4楼-- · 2020-02-26 00:55

When is working for me is to compute the constraintSize based on the width of the table view:

CGSize constraintSize = CGSizeMake(tableView.frame.size.width * 0.6, 2009);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
查看更多
唯我独甜
5楼-- · 2020-02-26 01:04

If you want to calculate the height properly for cellvalue2 you should use: [UIFont boldSystemFontOfSize:15.0f] and as linebreakmode: NSLineBreakByTruncatingTail

If you don't use bold, the text will fill correctly but you will lose the correct vertical padding.

The rest of your calculation are correct.

查看更多
Luminary・发光体
6楼-- · 2020-02-26 01:07

It seeems you've figured out that you were using the wrong text size. You might want to just use the font and lineBreakMode properties of the label to avoid this problem in the future, especially if you change them in the cell at a later time. Also, for readability's sake, I would avoid adding to the height before returning a number. Instead I'd try something like this:

CGSize textSize = CGSizeMake( 300.0, 1979 );
CGSize size = [ myTextString1 sizeWithFont:[[ cell textLabel ] font ]
                         constrainedToSize:textSize
                             lineBreakMode:[[ cell textLabel ] lineBreakMode ]];

result = MAX( size.height + 30, 44.0f );

I used 1979 because according to the documentation, one should not return values larger than 2009.

查看更多
疯言疯语
7楼-- · 2020-02-26 01:10

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;}
查看更多
登录 后发表回答