<legend id='4FATI'><style id='4FATI'><dir id='4FATI'><q id='4FATI'></q></dir></style></legend>
      1. <i id='4FATI'><tr id='4FATI'><dt id='4FATI'><q id='4FATI'><span id='4FATI'><b id='4FATI'><form id='4FATI'><ins id='4FATI'></ins><ul id='4FATI'></ul><sub id='4FATI'></sub></form><legend id='4FATI'></legend><bdo id='4FATI'><pre id='4FATI'><center id='4FATI'></center></pre></bdo></b><th id='4FATI'></th></span></q></dt></tr></i><div id='4FATI'><tfoot id='4FATI'></tfoot><dl id='4FATI'><fieldset id='4FATI'></fieldset></dl></div>

      2. <small id='4FATI'></small><noframes id='4FATI'>

        <tfoot id='4FATI'></tfoot>
          <bdo id='4FATI'></bdo><ul id='4FATI'></ul>
      3. “执行 -layoutSubviews 后仍需要自动布局"带有 UITableViewCell 子类

        时间:2023-09-13
            <bdo id='grf70'></bdo><ul id='grf70'></ul>
          • <i id='grf70'><tr id='grf70'><dt id='grf70'><q id='grf70'><span id='grf70'><b id='grf70'><form id='grf70'><ins id='grf70'></ins><ul id='grf70'></ul><sub id='grf70'></sub></form><legend id='grf70'></legend><bdo id='grf70'><pre id='grf70'><center id='grf70'></center></pre></bdo></b><th id='grf70'></th></span></q></dt></tr></i><div id='grf70'><tfoot id='grf70'></tfoot><dl id='grf70'><fieldset id='grf70'></fieldset></dl></div>

                <tbody id='grf70'></tbody>
              • <tfoot id='grf70'></tfoot>

                <small id='grf70'></small><noframes id='grf70'>

                <legend id='grf70'><style id='grf70'><dir id='grf70'><q id='grf70'></q></dir></style></legend>

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

                  问题描述

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

                  上一篇:什么是“限制边距"?在 Xcode 6 的情节提要中 下一篇:什么是 NSLayoutConstraint“UIView-Encapsulated-Layout-Height&quo

                  相关文章

                  <legend id='Zp0X5'><style id='Zp0X5'><dir id='Zp0X5'><q id='Zp0X5'></q></dir></style></legend>
                1. <tfoot id='Zp0X5'></tfoot>
                    <bdo id='Zp0X5'></bdo><ul id='Zp0X5'></ul>

                    1. <small id='Zp0X5'></small><noframes id='Zp0X5'>

                      <i id='Zp0X5'><tr id='Zp0X5'><dt id='Zp0X5'><q id='Zp0X5'><span id='Zp0X5'><b id='Zp0X5'><form id='Zp0X5'><ins id='Zp0X5'></ins><ul id='Zp0X5'></ul><sub id='Zp0X5'></sub></form><legend id='Zp0X5'></legend><bdo id='Zp0X5'><pre id='Zp0X5'><center id='Zp0X5'></center></pre></bdo></b><th id='Zp0X5'></th></span></q></dt></tr></i><div id='Zp0X5'><tfoot id='Zp0X5'></tfoot><dl id='Zp0X5'><fieldset id='Zp0X5'></fieldset></dl></div>