在iOS8上的表格视图单元格自动版式 [英] Table View Cell AutoLayout in iOS8

查看:190
本文介绍了在iOS8上的表格视图单元格自动版式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法得到自动版式在我的表视图细胞的工作。

在某些细胞似乎工作,并在别人看来不行。均匀完全相同的一种细胞。

On some cells it seems to work, and on others it seems to not work. Even cells of the exact same kind.

例如,在某些细胞中的描述将超过1行文本的价值的,它会正常工作...

For example, on some cells the Description will be more than 1 lines worth of text and it will work correctly...

...然而在其他细胞的描述将超过1行文本的价值,但只显示1行它与一堆空的空间。

...Yet on other cells the Description will be more than 1 lines worth of text but only show 1 line of it with a bunch of empty space.

你能不能帮我找出什么我丢失或做错了什么?谢谢!

我用这个计算器的问题,指导我的过程作为第一个定时器这样做:<一href=\"http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights\">Using自动布局中的UITableView动态单元布局和放大器;可变行高度

I'm using this StackOverflow question to guide my process as a first-timer doing this: Using Auto Layout in UITableView for dynamic cell layouts & variable row heights

1。设定与;添加约束

这些是因为我相信在大多数情况下工作良好。

These are working well for the most part I believe.

2。确定唯一表视图小区重用标识符

我不完全确信,如果我需要担心这个部分,因为我将永远有一个标题,时间和说明。

I'm not totally sure if I need to worry about this part since I will always have a Headline, Time, and Description.

对于iOS 8 - 自上浆细胞
3.启用行高度估计

我已将此添加 viewDidLoad中

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

更新:每Acey要求增加更多的信息

UPDATE: Adding more info per Acey request

要清楚,我把约束:


  • 标题:15左边,85顶,右15

  • 标题和时间之间的垂直间距,10

  • 时间和说明之间的垂直间距,10

  • 我Cmd的点击这三个标签,并增加叶片前缘和尾

  • 我说明和表格视图单元格的底部
  • 之间的牵制20
  • Headline: 15 left, 85 top, 15 right
  • Vertical Spacing between Headline and Time, of 10
  • Vertical Spacing between Time and Description, of 10
  • I Cmd clicked all three labels and added Leading Edges and Trailing Edges
  • I pinned 20 between Description and the bottom of the Table View Cell

更新2:解决
回答以下的工作非常出色,而且任何额外的间距是由于高度的细胞是太大设置,因此X code的自动添加额外的空间填写单元格的高度,因为文字标签并没有填写完整的高度的表格视图单元格。

UPDATE 2: Solved Answer below worked really well, but also any extra spacing was due to height set for cell being too large, so Xcode was automatically adding extra space to fill out height of cell since text labels didn't fill out the full height of the Table View Cell.

让我知道如果您有任何疑问或需要任何帮助,如果你遇到这一点,有同样的问题。

Let me know if you have any questions or need any help on this if you come across this and have the same problem.

谢谢大家!

推荐答案

使用新的iOS我还没有尝试尚未8的机制。但我面对我的时候,用的iOS 6/7,这样更新到iOS 8它仍然能正常工作的应用程序后类似的问题,所以也许老办法仍然是最好的方法是什么?

I haven't tried using the new iOS 8 mechanisms yet. But I have faced similar issues when I was doing this with iOS 6 / 7. After updating the app to iOS 8 it still works fine, so maybe the old way is still the best way?

我有我的code这里的一些例子:
<一href=\"http://stackoverflow.com/questions/23398042/autolayout-multiline-uilabel-cutting-off-some-text\">AutoLayout多行的UILabel切断一些文本

I have some examples of my code here: AutoLayout multiline UILabel cutting off some text

在这里:
<一href=\"http://stackoverflow.com/questions/23496547/autolayout-uitableviewcell-in-landscape-and-on-ipad-calculating-height-based-on\">AutoLayout在UITableViewCell的景观和iPad上的高度计算基于肖像iPhone

长话短说了pre的iOS 8路涉及保持细胞的复制只是计算在高度的tableView:heightForRowAtIndexPath:。但是,这是不够的,处理多行的UILabel 的。我不得不继承的UILabel 更新 preferredMaxLayoutWidth 每次 layoutSubviews 被调用。

Long story short the pre iOS 8 way involved keeping a copy of a cell just for calculating the height inside tableView:heightForRowAtIndexPath:. But this wasn't enough for dealing with multi line UILabel's. I had to subclass UILabel to update the preferredMaxLayoutWidth every time layoutSubviews was called.

preferredMaxLayoutWidth 修复似乎是神奇秘我失踪了。一旦我这样做了我的大部分细胞的完美工作。

The preferredMaxLayoutWidth "fix" seemed to be the magic secret I was missing. Once I did this most of my cells worked perfectly.

第二个问题我只需要我正确设置内容融为一体pression性和内容的拥抱性能,因此,例如告诉标签拥抱文本将意味着它不会扩大,以填补空白这将导致细胞萎缩。

The second issue I had only required me to set the content compression resistance and content hugging properties correctly, so for example telling the label to hug the text will mean it won't expand to fill the whitespace which will cause the cell to shrink.

在我做这2件事我现在细胞处理任何字体大小或任意数量的文本没有任何凌乱的布局code。这是一个很多东西需要学习,但我不认为它最终得到了回报,正如我在我的应用程序有很多动态的内容。

Once I did these 2 things my cells now handle any font size, or any amount of text without any messy layout code. It was a lot to learn but I do think it paid off in the end, as I have a lot of dynamic content in my app.

修改

在我自己的一些问题,未来与iOS 8后,我加入一些更多的细节来解决这些非常奇怪的autoLayout的bug。

After coming across a few issues of my own with iOS 8, i'm adding some more details to solve these very odd autoLayout bugs.

随着code我所提到的,它似乎并不在小区行高没有设置为自定义的工作。此设置在IB通过选择单元格,然后单击该选项卡的autoLayout(其中所有的内容融为一体pression性设置等都是)找到。 preSS的复选框,将填补一个临时的高度。

With the code I mentioned, it doesn't seem to work when the cell "Row Height" is not set to custom. This setting is found in IB by selecting the cell and clicking the autoLayout tab (where all the content compression resistance settings etc are). Press the checkbox and it will fill with a temporary height.

二是,在我的code我一直在小区的一个本地副本,然后重复使用它多次 heightForRowAtIndexPath里面:方法。这似乎受了不少每次调用时增加细胞的高度。我已经通过调用重新初始化本地副本:

Second is, in my code I keep a local copy of a cell, and then reuse it many times inside the heightForRowAtIndexPath: method. This seems to increase the cell height by a lot every time it is called. I had to re-init the local copy by calling:

localCopy = [self.tableView dequeueReusableCellWithIdentifier:@"mycell"];

看起来全新的X code 6 / iOS 8的变化非常多所以没与iOS 7的向下兼容性,它似乎是完全不同的管理。

It appears the new Xcode 6 / iOS 8 changes are very much so not backwards compatible with iOS 7 and it seems to be managed quite differently.

希望这有助于。

编辑2

看完这个问题后: iOS的自动版式多行的UILabel

我遇到的另一个问题与iOS 7/8的iOS支持自动布局!我重写 layoutSubviews 适用于iOS 8我还需要重写的setBounds 更新 preferredMaxLayoutWidth 调用super后。 WTF有苹果改变了!

I've come across another issue with iOS 7 / iOS 8 autolayout support!!! I was overriding layoutSubviews for iOS 8 I also needed to override setBounds to update the preferredMaxLayoutWidth after calling super. WTF have apple changed!

似乎是在为IB preferredMaxLayoutWidth 设置问题,因为iOS的7不能使用自动功能,如果使用相同的的UILabel 在多个设备上,它只会使用1宽度。所以,在iOS 8平板,因为同一个细胞都需要有在iOS 8 iPhone。

Seems to be an issue with the setting in IB for preferredMaxLayoutWidth, because iOS 7 can't use the automatic feature, if you use the same UILabel on multiple devices, its only going to use the 1 width. So UITableViewCell's on an iOS 8 tablet will be bigger because the same cell needs to have 2 lines on an iOS 8 iPhone.

这篇关于在iOS8上的表格视图单元格自动版式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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