l的舍入误差与NSAutoresizingMaskLayoutC

l的舍入误差与NSAutoresizingMaskLayoutC

本文介绍了UITableViewCell的舍入误差与NSAutoresizingMaskLayoutConstraint,但在规模和故事板正确heightForRowAtIndexPath设置:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用自动版式来配置我的表视图细胞,在理想情况下的子视图想表视图细胞是一样高足以包含所有子视图。然而,这并不似乎是可能的,因为在实际创建单元之前的小区的高度被确定。

I am trying to use AutoLayout to configure the subviews in my table view cells and in the ideal case would like the table view cell to be just as high as necessary to contain all the subviews. However, that does not seems to be possible, since the height for a cell is determined before the cell is actually created.

所以,现在,我只是看着我设置了约束和计算含有的一切,并设置在故事板的行高度为这一特定TableViewCell原型细胞所需的总高度,我在也返回此高度的tableView:heightForRowAtIndexPath:

So, for now, I just looked at the constraints I set up and calculated the total height needed to contain everything and set that in the storyboard as the row height for this specific TableViewCell prototype cell and I also return this height in the tableView:heightForRowAtIndexPath: method.

现在,我的表视图细胞看起来像这样(截图来自故事板):

Now, my table view cell looks like this (screenshot from storyboard):

有不都表现出了大小两个约束,它们都具有大小为10(按钮顶部容器用与滑块与中间的标签之间的距离)。

There are two constraints which don't have a size shown, they both have the size 10 (the button-top-container-view and the distance between the slider and the label in the middle).

从高端到低端的跟车距离去发生:

Going from top-to-bottom the following distance occur:


  • 10(距离)

  • 50(按钮的高度)

  • 20(距离)

  • 20(标签的高度)

  • 10(距离)

  • 30(滑块的高度)

  • 20(距离)

,导致160的总高度。

leading to a total height of 160.

这正是我在这个小区的督察已设置:

That's exactly what I have set in the inspector of this cell:

但你可以从第一张截图看到,界面生成器抱怨说,约束矛盾的(这就是为什么他们都显示为红色)。 IB是满意的,如果我将高度设置为161,但是这是错误的。

but as you can see from the first screenshot, Interface builder complains that the constraints are conflicting (that's why they are displayed in red). IB is satisfied if I set the height to 161, but that is wrong.

另外,如果我这样做,我会在运行时异常,由于冲突的约束:

Also, if I do, I get exceptions at runtime due to conflicting constraints:

Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x8d80240 V:[UISlider:0x8d81190(30)]>",
"<NSLayoutConstraint:0x8d833e0 V:[UILabel:0x8d83300(20)]>",
"<NSLayoutConstraint:0x8d83750 V:[UIButton:0x8d83600(50)]>",
"<NSLayoutConstraint:0x8d85050 V:|-(10)-[UIButton:0x8d83600]   (Names: '|':UITableViewCellContentView:0x8d80ef0 )>",
"<NSLayoutConstraint:0x8d851d0 V:[UILabel:0x8d83300]-(10)-[UISlider:0x8d81190]>",
"<NSLayoutConstraint:0x8d85200 V:[UISlider:0x8d81190]-(20)-|   (Names: '|':UITableViewCellContentView:0x8d80ef0 )>",
"<NSLayoutConstraint:0x8d85320 V:[UIButton:0x8d83600]-(20)-[UILabel:0x8d83300]>",
"<NSAutoresizingMaskLayoutConstraint:0x8d8b7a0 h=--& v=--& V:[UITableViewCellContentView:0x8d80ef0(161)]>"

最后一个是唯一一个我没有明确确定,但它似乎是从该行高度在故事板设置产生的,因为我的的tableView:heightForRowAtIndexPath:方法返回160:

the last one is the only one I did not explicitly set, but it seems to be generated from the row-height setting in the storyboard, since my tableView:heightForRowAtIndexPath: method returns 160:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == self.fetchedItemSetsController.fetchedObjects.count - 1)
    {
        return 160;
    }
    return 44;
}

(目前我总是希望最后一个单元格是大和特别的一个)

(at the moment I always want the last cell to be the large and special one)

那么,好吧,IB抱怨当我设置行高到160,但是当我将它设置为161,运行时抱怨。所以,我试图忽视IB和故事板设定的行高至160,以及(如被看见在第二截图)。在这种情况下,我得到一个微小的差别同样的错误信息:

So, okay, IB complains when I set the row height to 160, but when I set it to 161 the runtime complains. So I tried ignoring IB and set the row height to 160 in the storyboard as well (as seen in the second screenshot). In that case, I get the same error message with a tiny difference:

"<NSLayoutConstraint:0x17809ce80 V:[UISlider:0x125613660(30)]>",
"<NSLayoutConstraint:0x1782814f0 V:[UILabel:0x125615100(20)]>",
"<NSLayoutConstraint:0x178281a90 V:[UIButton:0x125615b50(50)]>",
"<NSLayoutConstraint:0x178281b80 V:|-(10)-[UIButton:0x125615240]   (Names: '|':UITableViewCellContentView:0x178161980 )>",
"<NSLayoutConstraint:0x178281c20 UIButton:0x1256157b0.height == UIButton:0x125615240.height>",
"<NSLayoutConstraint:0x178281d10 UIButton:0x1256157b0.height == UIButton:0x125615980.height>",
"<NSLayoutConstraint:0x178281e00 V:[UILabel:0x125615100]-(10)-[UISlider:0x125613660]>",
"<NSLayoutConstraint:0x178281e50 V:[UISlider:0x125613660]-(20)-|   (Names: '|':UITableViewCellContentView:0x178161980 )>",
"<NSLayoutConstraint:0x178281f40 UIButton:0x125615980.height == UIButton:0x125615b50.height>",
"<NSLayoutConstraint:0x178282030 V:[UIButton:0x125615240]-(20)-[UILabel:0x125615100]>",
"<NSAutoresizingMaskLayoutConstraint:0x178283340 h=--& v=--& V:[UITableViewCellContentView:0x178161980(159.5)]>"

有关表格视图单元格的高度约束现在是159.5,而不是160.我也遇到它作为前160.5,用同样的故事板的设置,但我不知道在哪里的来的。

The constraint for the height of the table view cell is now 159.5 instead of 160. I also encountered it as 160.5 before, with the same storyboard setting, but I am not sure where that came from.

所以,首先我认为这只是在IB显示错误,但现在它实际上似乎是在错误的约束。它创建了160.5(159.5或)约束,而不是我规定的160。 这是为什么?我能做些什么?

So, first I thought it was just a display bug in IB, but now it actually seems to be creating the wrong constraint. It creates a constraint with 160.5 (or 159.5) instead of the 160 I specified. Why is that? What can I do about it?

BTW:这些细胞似乎是正确显示,但我还是那句话怀疑我将能够看到一个0.5分的差距。主要是,我想摆脱例外,因为它们使调试非常困难,但我也想知道是怎么回事。

Btw: The cells seems to be displayed correctly, but then again I doubt I would be able to see a 0.5 point difference. Mainly, I would like to get rid of the exceptions, since they make the debugging much harder, but I would also like to know what is going on here.

更新:
在159.5不仅仅是由于故事板设置我只注意到,这是故事板设置的一个奇怪的组合和的返回值的tableView:heightForRowAtIndexPath:
下面是根据故事板高度(SB)产生的autoresizingmasklayoutconstraint的高度设置和几个例子的tableView的返回值:heightForRowAtIndexPath:方法(Method)。在所有情况下,子视图的限制应导致160的高度,并且不改变。

UPDATE:The 159.5 is not JUST due to the storyboard setting I just noticed, it is a strange combination of the storyboard setting and the return value of the tableView:heightForRowAtIndexPath:.Here are a few examples of the height of the generated autoresizingmasklayoutconstraint depending on the storyboard height (sb) setting and the return value of the tableView:heightForRowAtIndexPath: method (method). In all cases, the constraints of the subview should lead to a height of 160 and are not changed.


  1. 160(SB)及160(方法):159.5(约束)

  2. 161(SB)及160(方法):159.5(约束)

  3. 160(SB)及161(方法):160.5(约束)

  4. 162(SB)及161(方法):162(约束)

  5. 161(SB)及162(方法):161(约束)

  6. 162(SB)及162(方法):162(约束)

所以我想,好吧,对于(6),他们都同意,我只会让标签的高度22,所以要根据子视图的总高度为162也是如此。结果:

So I thought, okay, for (6.) they all agree, I will just make the label's height 22, so the total height according to the subviews will be 162 as well. Result:

6a上。 162(SB)及162(方法),162(子视图的总高度):161.5(约束)

6a. 162 (sb) & 162 (method), 162 (total height of subviews): 161.5 (constraint)

什么呢?!

任何想法这里发生了什么?

Any ideas what's happening here?

更新2
我只要再现Github上问题的示例项目。

Update 2I provided an example project that reproduces the issue on Github.

推荐答案

分隔符与高度搞乱了内容查看,或者它应该被禁用(和自定义的,如果需要更换),使内容查看高度单元的高度匹配或限制应改为更灵活的考虑到内容查看高度可能不匹配单元的高度。这样做既会更好。

Separator is messing with the height of the contentView, either it should be disabled (and replaced with custom one if required) so that the contentView height matches the height of the cell or the constraints should be changed to more flexible considering that the contentView height may mismatch the height of the cell. Doing both would be even better.

这篇关于UITableViewCell的舍入误差与NSAutoresizingMaskLayoutConstraint,但在规模和故事板正确heightForRowAtIndexPath设置:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!