如何解决,似乎没有定制来实现布局奇怪NSLayoutConstraint错误的UITableViewCell [英] How to fix strange NSLayoutConstraint errors that don't seem to effect layout in custom UITableViewCell

查看:552
本文介绍了如何解决,似乎没有定制来实现布局奇怪NSLayoutConstraint错误的UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每一次我的自定义UITableViewCells之一是通过绘制的tableView:的cellForRowAtIndexPath:控制台吐出一串NSLayoutConstraint不一致。我明白了其中的大部分:

Each time one of my custom UITableViewCells is drawn by tableView:cellForRowAtIndexPath: The console spits out a bunch of NSLayoutConstraint inconsistencies. I understand most of these:

 Unable to simultaneously satisfy constraints. ...boring stuff..

 "<NSLayoutConstraint:0x8b0eb60 V:|-(NSSpace(20))-[UILabel:0x8b0cb30]   (Names: '|':UITableViewCellContentView:0x8bd7d40 )>",
"<NSLayoutConstraint:0x8b0db70 V:[UILabel:0x8b0cb30]-(NSSpace(8))-[UITextView:0x91dba00]>",
"<NSLayoutConstraint:0x8b0dba0 V:[UITextView:0x91dba00]-(NSSpace(20))-|   (Names: '|':UITableViewCellContentView:0x8bd7d40 )>",
"<NSLayoutConstraint:0x8b0d5a0 V:[UITextView:0x91dba00(1000)]>",
"<NSAutoresizingMaskLayoutConstraint:0x8b00330 h=--& v=--& V:[UITableViewCellContentView:0x8bd7d40(44)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x8b0db70 V:[UILabel:0x8b0cb30]-(NSSpace(8))-[UITextView:0x91dba00]>

或者至少我觉得我做的。真正困扰我的是,我的UITableViewCell是工作,没有任何限制似乎被打破。我认为错误,尤其是最后一个上市的一些限制,是由系统添加的约束。这是可能的,如果我不使用.xibs和只添加了code的约束?在错误的最后一行伸出我的特别,因为我动态生成每个code中的高度,但我发现在那里的44个,默认的单元格高度。

Or at least I think I do. What really puzzles me is that my UITableViewCell is working and no constraints seem to be broken. I think some constraints listed in the error, particularly the last one, are constraints added by the system. Is this possible if I don't use .xibs and have only added constraints in code? The last line in the error sticks out to me in particular because I'm dynamically generating the height for each code but I noticed the 44 in there, the default cell height.

是一些这些约束添加默认情况下,例如说,当我打电话 [超级updateConstraints] ?我将如何去解决这些错误,找出他们是从哪里来的?

Are some of these constraints added by default, say for example when I call [super updateConstraints]? How would I go about resolving these errors or finding out where they are coming from?

在一个侧面说明,我的理解是浸入核心文本进行比我的UITextView +自动布局解决方案要好得多。现在我工作的缓存单元格高度。但是,可以将这些布局错误会造成延迟,而滚动或者是仅仅是因为我使用自动布局来计算每个单元的高度,因为它涉及在屏幕上?

On a side note, I understand that dipping into Core Text performs much better than my UITextView + Auto Layout solution. For now I'm working on caching cell heights. However, could these layout errors be causing lag while scrolling or is that simply because I'm using Auto Layout to calculate each cell height as it comes on screen?

我已经href=\"https://github.com/shawnthroop/Post-UITableViewCells\">项目中,这些错误在Github上的,如果有人想下载和体验的怪事发生张贴

I've posted the project in which these errors are occurring on Github if anyone wants to download and experience the weirdness for themselves.

推荐答案

根据阿龙的建议,我手动更改初始化时,这样的限制就可以在自动布局做了计算,在统治它的单元格的高度。如果没有初始设置的内容框架,默认帧 {0,0,320,44} 。这是太小,不能满足约束和错误在控制台显示。

As per Aaron's suggestion I manually change the height of the cell when it is initialized so that the constraints can then reign it in when Auto Layout does its calculations. Without setting the content frame initially, the default frame is {0, 0, 320, 44}. This is too small and cannot satisfy the constraints and errors show up in the console.

同样,再利用细胞的时候,老contentView.frame(通过自动布局计算)坚持围绕。这是好的,如果你使用的文字相同数量的 - 或更少 - 但如果新电池需要有更多的文字(因此更大contentView.frame),我们遇到同样的问题,contentView.frame太小不能满足该约束。

Similarly, when reusing cells the old contentView.frame (calculated by Auto Layout) sticks around. This is fine if you're using same amount of text - or less - but if the new cell needs to have more text (and therefore a bigger contentView.frame) we run into the same problem, the contentView.frame is too small and can't satisfy the constraints.

所以,在我的自定义的UITableViewCell我现在手动设置contentView.frame.height在 initWithStyle: prepareForReuse:来够恒大以适应任意数量的文本(足够的App.net职位和一些额外的余量)。这将确保没有错误时,自动布局做了计算。

So, in my custom UITableViewCell I now manually set the contentView.frame.height in initWithStyle: and prepareForReuse: to a constant big enough to accommodate any amount of text (enough for an App.net post and some extra headroom). This ensures no errors when Auto Layout does its calculations.

我甚至会想高一点,甚至可以动态地设置此值,在iOS中7,以适应动态文本至于滚动,它出现在错误并没有加剧波涛汹涌的滚动,那都是因为汽车布局计算(我认为)。下一步是计算单元格高度时, viewDidAppear:被称为...

I might even think about setting this value a little higher, or even dynamically, to accommodate for Dynamic Text in iOS 7. As for scrolling, it appears the errors didn't exacerbate the choppy scrolling, thats all because of Auto Layout calculations (I think). Next step is to calculate cell heights when viewDidAppear: is called...

这篇关于如何解决,似乎没有定制来实现布局奇怪NSLayoutConstraint错误的UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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