iOS8 使用 Interface Builder 自行调整大小的静态 TableView 单元格

iOS8 self-sizing static TableView cells with Interface Builder(iOS8 使用 Interface Builder 自行调整大小的静态 TableView 单元格)

本文介绍了iOS8 使用 Interface Builder 自行调整大小的静态 TableView 单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很高兴能够在 Xcode 中构建我的静态 TableViews,使用自动布局来支持动态类型,如

这是一个

此外,当我在标签或图像视图上设置显式高度约束时,我会生成以下内容.问题似乎是我没有设置的UIView-Encapsulated-Layout-Height"约束.我猜它的生成是因为在 IB 的静态表格视图中,行高设置(默认)为 44.

无法同时满足约束...."

但是 ViewDidLoad 中的 this 不应该覆盖 this 吗?

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

自动调整大小的表格视图单元格是否根本不适用于静态表格视图?

更新 2

我使用动态原型而不是静态表格视图构建了相同的东西,并且一切正常:

它确实似乎是静态与动态原型表.静态表格视图单元格未正确展开.

我没有看到 WWDC 视频中提到动态与静态,所以在这一点上,我将其归结为一个错误.如果我学到任何新东西会更新.

我更新了上面的 GitHub 存储库,以显示静态(损坏)和动态(工作)表视图的示例.

解决方案

答案:

这是 Xcode7b5 中的一个错误.我刚刚按照 Xcode 6.4 和 Xcode 7b5 中的相同步骤确认了这一点.

  1. 创建新的单一视图应用程序
  2. 新建文件.Cocoa 类MyStaticTableViewController",UITableViewController 的子类.
  3. 在 Storyboard 中,拖入新的 Table View Controller.
  4. 将类更改为 MyStaticTableViewController.
  5. 将故事板入口点拖到新的 VC.
  6. 删除原VC.
  7. 将新 VC 的类更改为 MyStaticTableViewController.
  8. 选择表格视图.将内容更改为静态单元格并将样式更改为分组.
  9. 将新标签拖到第一个表格视图单元格.将字体更改为正文.将顶部、底部、左侧、右侧约束设置为 0 并更新帧.
  10. 在 MyStaticTableViewController.m 中,添加:

<块引用>

 - (void)viewDidLoad {[超级视图DidLoad];self.tableView.rowHeight = UITableViewAutomaticDimension;self.tableView.estimatedRowHeight = 44.0;}

  1. 使用动态类型验证增加大小也会导致表格行扩展.

Xcode 6.4 创建的项目按预期工作 - 表格视图单元格展开以适合标签.

Xcode 7b5 创建的项目如原问题所述失败.

有趣的是,如果使用 Xcode7 打开 Xcode6 项目,它也可以正常工作.这与 Xcode7 如何创建项目或故事板有关.

更新

我比较了 .storyboard 文件,在损坏的 Xcode7b5 的 tableView 条目下,定义了一个 rect:

我删除了这一行,现在表格视图单元格似乎可以正确调整大小了!

更新 2

关键问题似乎是 XML 文件中每个tableViewCell"条目下留下的rect"条目.破解这些就是解决这个问题的方法.

更新 3

归档雷达:

工程部门已确定您的错误报告 (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 使用 Interface Builder 自行调整大小的静态 TableView 单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:iOS8 使用 Interface Builder 自行调整大小的静态 TableView 单元格

基础教程推荐