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

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

问题描述

使用 XCode 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 实现需要调用 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 方法,即使我尝试这样做并添加超级调用,因为它表明我仍然遇到相同的错误.如果我从 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,这是自动布局的问题.我很想知道将以下类别放入项目中是否能解决问题.它帮助了一个测试项目.

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 子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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