更新UITableViewCell子视图的frame没有效果 [英] Updating UITableViewCell subview's frame has no effect

查看:29
本文介绍了更新UITableViewCell子视图的frame没有效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义(子类化)UITableViewCell,其中包含一些 UILabels、一个 UIButton 和一个 NSBlock 作为属性.这个子类叫做ExploreCell.其中两个属性是 UILabels,分别命名为 waitLabellastUpdateLabel.

I have a custom (subclassed) UITableViewCell which contains a few UILabels, a UIButton, and a NSBlock as properties. This subclass is called ExploreCell. Two of the properties are UILabels and are named waitLabel and lastUpdateLabel respectively.

lastUpdateLabel 的内容为nil 或为空即@"" 时,我需要将waitLabel 垂直向下移动(在Y 轴)10 个像素.我通过检查 NSDictionary 中的一些对象来做到这一点,如下面的代码所示,我已将其放入 -tableView:cellForRowAtIndexPath: 方法中.

When the contents of lastUpdateLabel is nil or is is blank i.e. @"", I need to move the waitLabel vertically down (on the Y axis) 10 pixels. I am doing this by checking some objects in an NSDictionary as shown in the following code which I have put in the -tableView:cellForRowAtIndexPath: method.

CGRect frame = cell.waitLabel.frame;

if ([[venue allKeys] containsObject:@"wait_times"] && ([[venue objectForKey:@"wait_times"] count] > 0)) {

    frame.origin.y = 43;

}

else {
    frame.origin.y = 53;
}

[cell.waitLabel setFrame:frame];

然而,这段代码间歇性地工作,并且尝试调用 -setNeedsLayout,仍然不起作用.间歇性地,我的意思是在滚动几次后,符合将单元格原点向下移动 10 个像素的条件的一两个单元格实际上改变了它们的 waitLabel 的框架.

However, this code intermittently works, and having tried to call -setNeedsLayout, still does not work. By intermittently, I mean that after scrolling a few times, one or two of the cells that match the criteria to move the cell's origin down 10 pixels, actually have their waitLabel's frame changed.

请告诉我为什么会发生这种情况,以及如何解决.

Please can you tell me why this is occurring, and how it can be fixed.

推荐答案

这看起来像是自动布局的问题.如果您没有明确将其关闭,则它已打开.您应该为单元格顶部(或底部)的约束创建一个出口,并在代码中更改该约束的常量,而不是设置框架.根据 WWDC 2012 视频,如果您使用自动布局,则不应在代码中包含任何 setFrame: 消息.

This looks like a problem with auto layout. If you didn't explicitly turn it off, then it's on. You should make an outlet to a constraint to the top (or bottom) of the cell and change the constant of that constraint in code rather than setting frames. According to the WWDC 2012 videos, you shouldn't have any setFrame: messages in your code if you're using auto layout.

这篇关于更新UITableViewCell子视图的frame没有效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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