ios7中的Autofit表视图单元。

时间:2022-08-05 22:23:40

I know this question has been asked many times, but I'm struggling to find correct iOS7 implementations.

我知道这个问题已经被问过很多次了,但是我正在努力寻找正确的iOS7实现。

I have a tableview that is populated by an array that retrieves JSON data from my server. This data could be any length. Right now I can only get it to 1 or 2 lines, nothing more.

我有一个tableview,它由一个数组填充,它从我的服务器检索JSON数据。这个数据可以是任意长度。现在我只能得到1或2行,仅此而已。

Does anyone have any tips on the correct heightForRowAtIndexPath implementation?

是否有人对正确的heightForRowAtIndexPath实现有任何提示?

Thanks

谢谢

EDIT (current code):

编辑(当前代码):

-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
// Retrieve the line for this index (from JSON)

NSString *line = [myArray objectAtIndex:indexPath.row];
CGSize size = CGSizeZero;

size = [line boundingRectWithSize: (CGSize){640, CGFLOAT_MAX} options: NSStringDrawingUsesLineFragmentOrigin
                             attributes: @{ NSFontAttributeName: [UIFont systemFontOfSize:18]} context: nil].size;
return ceilf(size.height);
}

1 个解决方案

#1


0  

You just need to implement heightForRowAtIndexPath properly, something like this

您只需要正确地实现heightForRowAtIndexPath,就像这样。

-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
    // Retrieve the line for this index (from JSON)
    NSString* line = ...

    // Measure it with UIKit
    // font is a UIFont* and should be prepared once when the view initialized
    // cellSize is a CGSize like (320, 10000), where 320 is the width of the cell, 10000 means we want it as high as needed
    CGSize sz = [line sizeWithFont:font constrainedToSize:cellSize lineBreakMode: NSLineBreakByWordWrapping];

    return sz.height;
}

For cellForRowAtIndexPath, simply make the label fit the whole cell.

对于cellForRowAtIndexPath,只需让标签适合整个单元格。

For more info on NSString UIKit Additions, see this. Note that the method [sizeWithFont:constrainedToSize:lineBreakMode:] is deprecated in iOS 7, and has been replaced by another one, but I am not sure how to use it :)

有关NSString UIKit添加的更多信息,请参见此。注意这个方法[sizeWithFont:constrainedToSize:lineBreakMode:]在ios7中被弃用,已经被另一个替换了,但是我不知道如何使用它:)

#1


0  

You just need to implement heightForRowAtIndexPath properly, something like this

您只需要正确地实现heightForRowAtIndexPath,就像这样。

-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
    // Retrieve the line for this index (from JSON)
    NSString* line = ...

    // Measure it with UIKit
    // font is a UIFont* and should be prepared once when the view initialized
    // cellSize is a CGSize like (320, 10000), where 320 is the width of the cell, 10000 means we want it as high as needed
    CGSize sz = [line sizeWithFont:font constrainedToSize:cellSize lineBreakMode: NSLineBreakByWordWrapping];

    return sz.height;
}

For cellForRowAtIndexPath, simply make the label fit the whole cell.

对于cellForRowAtIndexPath,只需让标签适合整个单元格。

For more info on NSString UIKit Additions, see this. Note that the method [sizeWithFont:constrainedToSize:lineBreakMode:] is deprecated in iOS 7, and has been replaced by another one, but I am not sure how to use it :)

有关NSString UIKit添加的更多信息,请参见此。注意这个方法[sizeWithFont:constrainedToSize:lineBreakMode:]在ios7中被弃用,已经被另一个替换了,但是我不知道如何使用它:)