首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从cellForRowAtIndexPath内部调用heightForRowAtIndexPath?

从cellForRowAtIndexPath内部调用heightForRowAtIndexPath?
EN

Stack Overflow用户
提问于 2013-02-19 19:20:12
回答 3查看 18.5K关注 0票数 19

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

我很困惑,如果设置高度的方法在计算它的方法之前被调用,我怎么才能正确地设置单元格的高度。

我已经创建了一个类别的NSString和另一个类别的UILabel,这两个类别一起工作使UILabel的大小合适。问题是包含这些标签的UITableViewCell实例没有调整大小,所以标签挂在每个标签的末尾,与下面的标签重叠。

我知道我需要做的是使单元格的高度等于标签的高度,加上一些额外的空白处,但我不明白如何在计算标签高度之前设置高度。我可以在不使用heightForRowAtIndexPath的情况下设置高度吗?或者,如果没有,我可以在其他地方计算单元格的高度吗?我目前正在使用indexPath.row来实现这一点,所以提前做这件事是很棘手的。下面是我的cellForRowAtIndexPath代码:

cellForRowAtIndexPath

代码语言:javascript
运行
复制
- (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;
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-02-19 19:27:57

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

代码语言:javascript
运行
复制
- (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来设置标注大小的一些约束

代码语言:javascript
运行
复制
#define kLabelFrameMaxSize CGSizeMake(265.0, 200.0)

然后,通过将固定高度与可变标签高度相加来返回高度。

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

代码语言:javascript
运行
复制
- (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;
}
票数 15
EN

Stack Overflow用户

发布于 2013-02-19 23:30:46

你应该用heightForRowAtIndexPath方法计算单元格的高度。它应该不使用任何UILabel,就像JP Hribovsek的答案中的heightForRowAtIndexPath实现一样。

然后,在cellForRowAtIndexPath中,不要设置任何子视图的框架,也不要调用标签的sizeToFitMultipleLines方法。相反,应实现FQCustomCelllayoutSubviews方法,以便为单元格的任何可能边界正确设置子视图框。在此方法中,您不应该关心文本或字体大小,只需确保标签的边距正确即可。如果在heightForRowAtIndexPath方法中正确计算了单元格的高度,那么标签的大小将恰到好处。

票数 1
EN

Stack Overflow用户

发布于 2013-02-19 19:32:13

对于每个单元格,首先调用heightforRowAtIndexPath,然后调用CellForRowAtIndexPath。因此,您可以先计算高度,然后在CellForRowAtIndexPath中填充每个单元格的数据。

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14956189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档