从cellForRowAtIndexPath中调用heightForRowAtIndexPath? [英] Calling heightForRowAtIndexPath from within cellForRowAtIndexPath?

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

问题描述

因此,似乎 heightForRowAtIndexPath cellForRowAtIndexPath 之前被调用。我目前正在使用 cellForRowAtIndexPath 来计算每个单元格的高度(它们是可变高度,使用 UILabel 其中包含不同数量的文本。

So, it seems that heightForRowAtIndexPath is called before cellForRowAtIndexPath. I'm currently using cellForRowAtIndexPath to calculate the height of each cell (they're variable heights, using a UILabel with varying amounts of text in it.

如果设置的方法,我对如何正确设置单元格的高度感到困惑在计算的方法之前调用height。

I'm puzzled as to exactly how I can set the cell's height correctly if the method that sets the height is called before the method that calculates it.

我创建了一个 NSString 和另一个 UILabel ,它们共同使 UILabel 的大小合适。问题在于 UITableViewCell 包含这些标签的实例未调整大小,因此标签会悬挂在每个标签的末尾,与下面的标签重叠。

I've created a category of NSString, and another of UILabel, which working together make the UILabel the right size. The issue is that the UITableViewCell instances which contain those labels aren't being resized, so the labels hang over the end of each label, overlapping the ones beneath.

我知道我需要做的是让单元格的高度等于标签的高度,加上一些额外的边距空间,但我很困惑如何在标签之前设置高度高度已计算出来。我可以设置高度而不使用 heightForRowAtIndexPath ?或者如果没有,我可以在其他地方计算出细胞的高度吗?我目前正在使用 indexPath.row 来实现这一点,所以事先做这件事会很棘手。这是代码我的cellForRowAtIndexPath:

I know that what I need to do is make the height of the cell equal the height of the label, plus some extra margin space, but I'm puzzled as to how I can set the height before the label height has been calculated. Can I set the height someone without using heightForRowAtIndexPath? Or if not, could I calculate the height of the cell elsewhere? I'm currently using indexPath.row to make that happen, so doing it beforehand would be tricky. Here's the code my cellForRowAtIndexPath:

cellForRowAtIndexPath

- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
{
    FQCustomCell *_customCell = [tableView dequeueReusableCellWithIdentifier: @"CustomCell"];
    if (_customCell == nil) {
        _customCell = [[FQCustomCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"CustomCell"];
    }

    _customCell.myLabel.text = [[_loadedNames objectAtIndex: indexPath.row] name];
    _customCell.myLabel.font = [UIFont fontWithName: @"FontA" size: 15.0f];

    UIView *backView = [[UIView alloc] initWithFrame: CGRectZero];
    backView.backgroundColor = [UIColor clearColor];
    _customCell.backgroundView = backView;

    _customCell.myLabel.frame = CGRectMake(5, 5, 265, 65);

    [_customCell.myLabel sizeToFitMultipleLines];
    return _customCell;
}


推荐答案

你需要计算高度 heightForRowAtIndexPath 中的单元格,类似于

You need to calculate the height of the cell in heightForRowAtIndexPath, something like

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *theText=[[_loadedNames objectAtIndex: indexPath.row] name];
    CGSize labelSize = [theText sizeWithFont:[UIFont fontWithName: @"FontA" size: 15.0f] constrainedToSize:kLabelFrameMaxSize];
    return kHeightWithoutLabel+labelSize.height;
}

您可以通过设置 kLabelFrameMaxSize

#define kLabelFrameMaxSize CGSizeMake(265.0, 200.0)

然后通过添加带有可变标签高度的常量高度来返回高度。

Then you return the height by adding a constant height with the variable label height.

另外,为了保持一致,您应该使用相同的方法在 cellForRowAtIndexPath 中设置框架,而不是使用 sizeToFitMultipleLines

Also, to be consistent, you should use the same methodology to set the frame in cellForRowAtIndexPath instead of using sizeToFitMultipleLines

- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
{
    FQCustomCell *_customCell = [tableView dequeueReusableCellWithIdentifier: @"CustomCell"];
    if (_customCell == nil) {
        _customCell = [[FQCustomCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"CustomCell"];
    }

    _customCell.myLabel.text = [[_loadedNames objectAtIndex: indexPath.row] name];
    _customCell.myLabel.font = [UIFont fontWithName: @"FontA" size: 15.0f];

    UIView *backView = [[UIView alloc] initWithFrame: CGRectZero];
    backView.backgroundColor = [UIColor clearColor];
    _customCell.backgroundView = backView;

    CGSize labelSize = [_customCell.myLabel.text sizeWithFont:_customCell.myLabel.font constrainedToSize:kLabelFrameMaxSize];
    _customCell.myLabel.frame = CGRectMake(5, 5, labelSize.width, labelSize.height);

    return _customCell;
}

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

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