iOS8上的自我调整大小的静态的TableView细胞界面生成器 [英] iOS8 self-sizing static TableView cells with Interface Builder

查看:210
本文介绍了iOS8上的自我调整大小的静态的TableView细胞界面生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是通过能在X code正确建立我的静态TableViews,使用自动布局,支持动态类型,如的 WWDC2014什么表的新的和集合视图

我倒是preFER使用严格X code /界面生成器和故事来处理自动布局。当我这样做,不知何故的TableView细胞不调整,我敢肯定,我失去了一些东西简单。

要重现此,使用X code 7测试版5,我:


  • 创建一个新的单一视图的项目。

  • 将故事板切入点,以新的UITableViewController。

  • 请其与分组的风格静态表视图。

  • 添加一个标签的第一行。添加的约束到顶部,底部,左侧和右侧。

  • 设置标签的字体是身体。

  • 添加到viewDidLoad中:

    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 44.0;


  • 运行它,并设置调整动态类型。而字体的变化,高度的TableView仍然停留在44表观默认并截断标签。


,在模拟器截断标签

下面是一个 GitHub的库这是上面的结果。

为什么不是自动布局造成的TableView扩大,填补了必要的空间,以适应新的更大的标签文本?

我已经使用code设置限制<一​​个发现关于这个问题的伟大的职位,如以下,具有弯曲href=\"http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights\">here.

我没有找到在纯Interface Builder中这样做的任何的例子。当然,这是完全可能的?

更新

我已经更新了GitHub的源代码,包括一个具有ImageView的,而不是一个UILabel第二表行;它显示了同样的问题:

更新截图

此外,当我设置任何标签或图像视图上的一个明确的高度约束,我生成以下。这个问题似乎是我没有设置这种UIView的封装,布局,高度的约束。我猜它的产生是因为在我的IB静态的tableview,行高设置(默认)至44

 无法同时满足约束条件。
...&所述; NSLayoutConstraint:0x7fa371f39e30五:[的UILabel:0x7fa371f38d30'Label'(20)] gt;中,
&所述; NSLayoutConstraint:0x7fa371c39160的UILabel:0x7fa371f38d30'Label'.top == UITableViewCellContentView:0x7fa371e17dc0.topMargin&gt;中,
&所述; NSLayoutConstraint:0x7fa371c393c0 UITableViewCellContentView:0x7fa371e17dc0.bottomMargin ==的UILabel:0x7fa371f38d30'Label'.bottom&gt;中,
&LT; NSLayoutConstraint:0x7fa371f41a40'的UIView封装 - 布局 - 身高V:[UITableViewCellContentView:0x7fa371e17dc0(43.5)]≥

但不是这viewDidLoad中应该重写此?

  self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 44.0;

做自我调整大小表视图细胞根本不具有静态表视图工作?

更新2

我已经建立了使用动态原型,而不是一个静态的表视图的同样的事情,一切工作正常:

在这里输入的形象描述

这确实似乎是静态与动态的原型表。静态表视图细胞不能正确展开。

我见过没有提到的动态与在WWDC视频静态的,所以在这一点上,我这个粉最多的错误。如果我知道任何新的东西将会更新。

我已经更新了GitHub的库上面同时显示静态的(碎)和动态(工作)表视图的一个例子。


解决方案

答:

这是在X code7b5的错误。我刚才遵循以x code 6.4和X code 7b5相同的步骤证实了这一点。


  1. 创建新的单一视图的应用

  2. 新文件。可可类MyStaticTableViewController的UITableViewController的子类。

  3. 在故事板,新表视图控制器拖累。

  4. 更改类到MyStaticTableViewController。

  5. 将故事情节切入点,以新的VC。

  6. 删除原始VC。

  7. 更改类以MyS​​taticTableViewController新的VC。

  8. 选择表视图。更改内容为静态细胞和风格来分组。

  9. 将新标签第一表格视图单元格。更改字体为主体。设置上,下,左,右限制为0和更新帧。

  10. 在MyStaticTableViewController.m,添加:


   - (无效)viewDidLoad中{
    [超级viewDidLoad中];    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 44.0;}



  • 与动态类型检查,增加的尺寸会导致表行扩大为好。

  • 用X code创建的项目6.4按预期工作 - 表格视图单元扩展到适合标签

    用X code创建的项目7b5在原来的问题描述失败。

    有趣的是,如果用开X code7的X code6的项目,它工作正常,以及。这件事情做X code7是如何创建项目,或故事板。

    更新

    我比较了.storyboard文件彼此,并且在破碎x code7b5之一的tableView条目下,存在定义一个矩形:

     &LT; RECT键=帧X =0.0Y =0.0WIDTH =600HEIGHT =600/&GT;

    我删除了这条线,这似乎表视图电池现在可以正确的自我大小!

    更新2

    问题的关键似乎是一个矩形项下的每个XML文件中的tableViewCell条目的离开了。重击那些是解决这个问题。

    更新3

    提起雷达:

    工程已经确定你的bug报告(22311571)是另一个问题(17995201),将封闭的副本。

    I am excited by the prospect of being able to build my static TableViews right within Xcode, using Auto Layout to support dynamic type, as mentioned in WWDC2014 What's New in Table and Collection Views.

    I'd prefer to use strictly Xcode / Interface Builder and storyboards to handle the Auto Layout. When I do, somehow the TableView cells are not resizing and I'm sure I'm missing something simple.

    To reproduce this, using Xcode 7 beta 5, I:

    • Created a new single view project.
    • Set the Storyboard entry point to a new UITableViewController.
    • Make it a static table view with grouped style.
    • Added a single label to the first row. Added constraints to the top, bottom, left, and right.
    • Set the label font to be "Body".
    • Added to ViewDidLoad:

      self.tableView.rowHeight = UITableViewAutomaticDimension; self.tableView.estimatedRowHeight = 44.0;

    • Ran it and adjusted Dynamic Type in Settings. While the font changes, the TableView height remains stuck at the apparent default of 44 and truncates the label.

    Here is a GitHub repository which is the result of the above.

    Why isn't Auto Layout causing the TableView to expand to fill the necessary space to fit the new larger label text?

    I have found great posts on the subject such as the following, with a bent on using code to set up constraints here.

    I'm not finding any examples of doing this in pure Interface Builder. Surely this is quite possible?

    Update

    I have updated the GitHub source to include a second table row with an ImageView instead of a UILabel; it shows the same issue:

    Additionally, when I set an explicit height constraint on either the label or image view, I generate the following. The problem seems to be this "UIView-Encapsulated-Layout-Height" constraint which I have not set. I'm guessing it's generated because in my static tableview in IB, the Row Height is set (as default) to 44.

    Unable to simultaneously satisfy constraints.
    ...
    
    "<NSLayoutConstraint:0x7fa371f39e30 V:[UILabel:0x7fa371f38d30'Label'(20)]>",
    "<NSLayoutConstraint:0x7fa371c39160 UILabel:0x7fa371f38d30'Label'.top == UITableViewCellContentView:0x7fa371e17dc0.topMargin>",
    "<NSLayoutConstraint:0x7fa371c393c0 UITableViewCellContentView:0x7fa371e17dc0.bottomMargin == UILabel:0x7fa371f38d30'Label'.bottom>",
    "<NSLayoutConstraint:0x7fa371f41a40 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fa371e17dc0(43.5)]>"
    

    But shouldn't this in ViewDidLoad be overriding this?

    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 44.0;
    

    Do self-sizing table view cells simply not work with static table views?

    Update 2

    I have built the same thing using a dynamic prototype instead of a static table view, and all works fine:

    It does indeed seem to be static versus dynamic prototyped tables. Static table view cells do not properly expand.

    I've seen no mention of dynamic vs. static in the WWDC videos, so at this point I'm chalking this up to a bug. Will update if I learn anything new.

    I have updated the GitHub repository above to show an example of both the static (broken) and dynamic (working) table views.

    解决方案

    Answer:

    This is a bug in Xcode7b5. I have just confirmed this by following the identical steps in Xcode 6.4 and Xcode 7b5.

    1. Create new Single View Application
    2. New File. Cocoa Class "MyStaticTableViewController", subclass of UITableViewController.
    3. In Storyboard, drag in new Table View Controller.
    4. Change Class to MyStaticTableViewController.
    5. Drag storyboard entry point to new VC.
    6. Delete original VC.
    7. Change Class to MyStaticTableViewController for new VC.
    8. Select the Table View. Change Content to Static Cells and Style to Grouped.
    9. Drag new label to first Table View Cell. Change Font to Body. Set top, bottom, left, right constraints to 0 and update frames.
    10. In MyStaticTableViewController.m, add:

     - (void)viewDidLoad {
        [super viewDidLoad];
    
        self.tableView.rowHeight = UITableViewAutomaticDimension;
        self.tableView.estimatedRowHeight = 44.0;
    
    }
    

    1. Verify with Dynamic Type that increasing the size causes the table row to expand as well.

    The project created by Xcode 6.4 works as expected - the table view cells expand to fit the label.

    The project created by Xcode 7b5 fails as described in the original question.

    Interestingly, if open the Xcode6 project with Xcode7, it works fine as well. It's something to do with how Xcode7 is creating the project, or storyboard.

    Update

    I compared the .storyboard files to each other, and in the broken Xcode7b5 one under the tableView entry, there is a rect defined:

    <rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
    

    I deleted this line and it seems that the table view cell now properly self-sizes!

    Update 2

    The key issue seems to be a "rect" entry left under each of the "tableViewCell" entries in the XML file. Whacking those is what fixes this issue.

    Update 3

    Filed radar:

    "Engineering has determined that your bug report (22311571) is a duplicate of another issue (17995201) and will be closed."

    这篇关于iOS8上的自我调整大小的静态的TableView细胞界面生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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