问题描述
使用 XCode 4.5 和 iOS 6,我正在开发一个带有自定义单元格的简单表格视图的应用程序.我在 iOS 5 及更低版本中已经这样做了一百次,但由于某种原因,新的自动布局系统给我带来了很多麻烦.
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 实现需要调用 super.'
*** 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 方法,即使我尝试这样做并添加 super 调用,因为它表明我仍然得到相同的错误.如果我从 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.
我几乎可以肯定我遗漏了一些简单的东西,但找不到任何文档或指南来说明我做错了什么.任何帮助将不胜感激.
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.
推荐答案
显然UITableViewCell的layoutSubviews实现没有调用super,是auto layout的问题.我很想看看将以下类别放入项目中是否可以解决问题.它帮助了一个测试项目.
Apparently, UITableViewCell's layoutSubviews implementation does not call super, which is a problem with auto layout. I'd be interested to see if dropping the below category into projects fixes things. It helped in a test project.
#import <objc/runtime.h>
#import <objc/message.h>
@implementation UITableViewCell (FixUITableViewCellAutolayoutIHope)
+ (void)load
{
Method existing = class_getInstanceMethod(self, @selector(layoutSubviews));
Method new = class_getInstanceMethod(self, @selector(_autolayout_replacementLayoutSubviews));
method_exchangeImplementations(existing, new);
}
- (void)_autolayout_replacementLayoutSubviews
{
[super layoutSubviews];
[self _autolayout_replacementLayoutSubviews]; // not recursive due to method swizzling
[super layoutSubviews];
}
@end
我可能会添加在表格单元格上使用 backgroundView 时出现的问题,因为它会作为子视图添加到单元格中(而大多数子视图应该添加到表格单元格的 contentView 中,这通常应该会更好).
I might add the problem showed up for me when using a backgroundView on the table cell, as that gets added as a subview to the cell (whereas most subviews should be added to the table cell's contentView, which should usually work better).
注意:这个bug似乎在iOS7中修复了;我能够删除此代码,或者至少添加一个运行时检查,以便仅在 iOS6 上运行时完成.
Note: It appears this bug is fixed in iOS7; I was able to remove this code, or at least add a runtime check so that it's only done if running on iOS6.
这篇关于“执行 -layoutSubviews 后仍需要自动布局"带有 UITableViewCell 子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!