【问题标题】:Updating a subclassed UILabel frame within setText: as UILabel does not resize in UITableViewCell until reload of cell在 setText 中更新子类 UILabel 框架:因为 UILabel 在重新加载单元格之前不会在 UITableViewCell 中调整大小
【发布时间】:2012-12-28 08:37:06
【问题描述】:

我对 UILabel 进行了子类化,我打算在许多不同的 UITableViewCells 中使用它。

setText: 内,我使用sizeWithFont 来获取UILabel 的动态高度。

这一切都很好,但是,子类UILabel 的框架在重绘单元格之前不会改变。我该如何克服这个问题?

注意:在下面的代码中,由于if 语句,重绘时大小不会改变。我把这个拿出来测试,重新绘制后发现它可以工作了。

我认为它需要使用 setNeedsDisplay 或类似的,但它们也不起作用。

这是我正在使用的代码:

- (void)setText:(NSString *)text
{
    if (text != _text) {
        //  Adjust size to fit contents
        //
        CGSize maximumSize = CGSizeMake(CGRectGetWidth(self.frame), CGFLOAT_MAX);
        CGSize predictedSize = [text sizeWithFont:self.font constrainedToSize:maximumSize lineBreakMode:self.lineBreakMode];

        NSLog(@"Predicted size for \"%@\" (width: %f) is %@", text, self.frame.size.width, NSStringFromCGSize(predictedSize));

        CGRect headlineDescriptionLabelFrame = self.frame;

        headlineDescriptionLabelFrame.size = predictedSize;

        NSLog(@"Old frame: %@", NSStringFromCGRect(self.frame));

        [self setFrame:headlineDescriptionLabelFrame];

        NSLog(@"New frame: %@", NSStringFromCGRect(self.frame));

        _text = text;
    }
}

我的输出看起来像这样(上面的 'if' 关闭):

2012-12-19 19:51:23.576 myApp[1099:c07] Predicted size for "Facebook's photo-sharing service Instagram denies that it has changed its privacy policy to allow it to sell users' photos to advertisers." (width: 172.000000) is {170, 75}
2012-12-19 19:51:23.578 myApp[1099:c07] Old frame: {{138, 46}, {172, 49}}
2012-12-19 19:51:23.578 myApp[1099:c07] New frame: {{138, 46}, {170, 75}}

在此处重新加载单元格
在第一次重新加载单元格后,UILabel 会调整大小并显示

2012-12-19 19:51:30.018 myApp[1099:c07] Predicted size for "Facebook's photo-sharing service Instagram denies that it has changed its privacy policy to allow it to sell users' photos to advertisers." (width: 172.000000) is {170, 75}
2012-12-19 19:51:30.018 myApp[1099:c07] Old frame: {{138, 46}, {172, 49}}
2012-12-19 19:51:30.019 myApp[1099:c07] New frame: {{138, 46}, {170, 75}}

在此处重新加载单元格

2012-12-19 19:51:32.014 myApp[1099:c07] Predicted size for "Facebook's photo-sharing service Instagram denies that it has changed its privacy policy to allow it to sell users' photos to advertisers." (width: 170.000000) is {170, 75}
2012-12-19 19:51:32.014 myApp[1099:c07] Old frame: {{138, 46}, {170, 75}}
2012-12-19 19:51:32.015 myApp[1099:c07] New frame: {{138, 46}, {170, 75}}

编辑

经过4天的尝试,我仍然遇到这个问题,有人知道这是否是Apple方面的错误?其他人如何在 cellForRowAtIndexPath: 内部调整从 UITableViewCell 笔尖加载的 UILabel 的大小?

【问题讨论】:

  • 你试过setNeedsLayout吗?
  • 是的,但还是同样的问题
  • 无论如何要'碰到'这个问题,我在任何地方都找不到解决问题的方法
  • 另外,我在 Interface Builder 中构建了 UITableViewCell。初始高度是 IB 中使用的高度,然后,当单元格重新绘制到屏幕上时,会显示正确的高度,即使它的框架说它最初设置为正确的高度
  • 你的tableView:heightForRowAtIndexPath:是什么样的?

标签: ios uilabel frame sizewithfont


【解决方案1】:

不要“在此处重新加载单元格”,只需尝试

[tableView beginUpdates];
[tableView endUpdates];

触发重新计算。没有真正重新加载单元格的高度,这是不必要的,因为您只更改了标签的文本。

【讨论】:

  • 感谢您的回复,当我不使用代码滚动 tableView 时,我的单元格正在“重新加载”。如果我要实现你的代码,放在哪里最好?
  • 正如我所说,而不是 cell reload here 在您粘贴的代码中。
  • 您的意思是在setText: 方法内吗?这行不通。注:我上面用Cell reload here粘贴的代码是从日志输出的
【解决方案2】:

事实证明,我需要做的只是将换帧代码添加到layoutSubviews(以及超级方法[super layoutSubviews]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多