"自动布局仍在执行-layoutSubviews&QUOT后需要;用的UITableViewCell子类 [英] "Auto Layout still required after executing -layoutSubviews" with UITableViewCell subclass

查看:273
本文介绍了"自动布局仍在执行-layoutSubviews&QUOT后需要;用的UITableViewCell子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用X code ++ 4.5和iOS 6,我正在开发与定制单元一个简单的表视图的应用程序。我这的iOS 5做了一百次及以下,但由于某些原因,新的autoLayout系统是给我带来很多麻烦。

Using XCode 4.5 and iOS 6, I'm developing an app with a simple table view with custom cells. I've done this a hundred times in iOS 5 and below, but for some reason the new autoLayout system is giving me a lot of trouble.

在IB设置我的表视图和原型细胞,添加子视图和有线它们作为IBOutlets然后设置我的委托和数据源。但是现在,只要第一个单元格为的cellForRowAtIndexPath 牵强,我得到以下错误:

I setup my table view and prototype cell in IB, added subviews and wired them up as IBOutlets then setup my delegate and dataSource. However now whenever the first cell is fetched from cellForRowAtIndexPath, I get the following error:

在***断言失败 - [ShopCell layoutSublayersOfLayer:],/SourceCache/UIKit_Sim/UIKit-2372/UIView.m:5776

*** Assertion failure in -[ShopCell layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2372/UIView.m:5776

***终止应用程序由于未捕获的异常'NSInternalInconsistencyException,理由是:自动布局仍在执行-layoutSubviews后需要。 ShopCell的实现-layoutSubviews的需要调用超。

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. ShopCell's implementation of -layoutSubviews needs to call super.'

我没有在我的子类细胞(ShopCell)实施了-layoutSubviews方法,甚至当我尝试这样做,并添加超级调用因为它表明我仍然得到同样的错误。如果我在IB细胞移除子视图,并将其更改为一个标准的UITableViewCell,一切正常,不过当然我留下了没有数据在我的细胞。

I haven't implemented a -layoutSubviews method in my subclassed cell (ShopCell), and even when I try to do that and add the super call as it suggests I still get the same error. If I remove the subviews from the cell in IB, and change it to a standard UITableViewCell, everything works as expected, though of course I'm left with no data in my cells.

我几乎可以肯定,有一些简单的我失踪,但无法找到任何文件或指导建议我做了什么错。任何帮助将是AP preciated。

I'm almost certain that there's something simple I'm missing, but can't find any documentation or guides to suggest what I've done wrong. Any help would be appreciated.

编辑:只是试图将其更改为在IB一个UITableViewCell并留下所有的子视图到位,仍​​是同样的错误

Just tried changing it to a UITableViewCell in IB and leaving all the subviews in place, still the same error.

推荐答案

我遇到同样的问题,而手动添加在code约束。在code,我是做了以下内容:

I encountered the same problem while manually adding constraints in code. In code, I was doing the following:

{
    [self setTranslatesAutoresizingMaskIntoConstraints:YES];
    [self addSubview:someView];
    [self addSubview:someOtherView];
    [self addConstraint:...];
}

假设

从我所知道的,问题是,当你停用 translatesAutoresizingMaskIntoConstraints ,一个UITableViewCell开始使用自动布局,自然失败,因为底层实现的 layoutSublayersForLayer 不叫超。有人用料斗或其他一些工具可以证实这一点。由于您使用的IB你可能想知道为什么这是一个问题......那是因为使用IB自动禁用 translatesAutoresizingMaskIntoConstraints 的意见,这增加了约束(它会自动加在他们的地方宽度和高度的限制)。

From what I can tell, the issue is that when you disable translatesAutoresizingMaskIntoConstraints, UITableViewCell starts to use Auto Layout and naturally fails because the underlying implementation of layoutSublayersForLayer does not call super. Someone with Hopper or some other tool can confirm this. Since you're using IB you're probably wondering why this is an issue... and that's because using IB automatically disables translatesAutoresizingMaskIntoConstraints for views that it adds constraints to (it will automatically add a width and height constraint in their place).

解决方案

我的解决办法是一切移动到内容查看

My solution was to move everything to the contentView.

{
   [self.contentView addSubview:someView];
   [self.contentView addSubview:someOtherView];
   [self.contentView addConstraint:...];
}

我不是100%肯定这是否会在Interface Builder工作,但如果你把一切都关掉你的手机中(假设你有直接在它的东西),那么它应该工作。希望这可以帮助您!

I'm not 100% sure if this will work in Interface Builder, but if you push everything off of your cell (assuming that you have something directly on it) then it should work. Hope this helps you!

这篇关于"自动布局仍在执行-layoutSubviews&QUOT后需要;用的UITableViewCell子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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