forkingdog / UITableView-FDTemplateLayoutCell

Template auto layout cell for automatically UITableViewCell height calculating
MIT License
9.94k stars 2.01k forks source link

ios 8.3 cell计算高度不准确 使用的是1.6 #315

Open kikicloud123 opened 7 years ago

zvving commented 7 years ago

昨天确认了下,有这个问题,详情在这里描述下。

原因

iOS 10.3 以后,如果调用系统的 systemLayoutSizeFittingSize 方法,系统会自动给 contentView 加 width height 两个约束,这两个约束的值跟 xib 的 view size 一样。

这就导致了自定义 cell 中一个可变高度 label 的宽度约束依赖 contentView 的宽度时,而 contentView 有个错误的宽度约束,导致计算错误。 FDTemplateLayoutCell 1.6 版本增加了 padding 约束,但是相比系统增加的宽度约束优先级 -1,不能解决这个问题。

重现方法:

解决方法

weekwood commented 7 years ago

@zvving nice issue template!

kikicloud123 commented 7 years ago

能修复这个问题吗

kikicloud123 commented 7 years ago

昨天测试的时候 在iphoneSE 上是好的 但是到了6s 7plus上 会出现cell 高度计算不准确的问题

kikicloud123 commented 7 years ago

使用的是1.6版本 label文字在中间 但是上下出现很多空白

kikicloud123 commented 7 years ago

刚才测试了一下 在大屏上还是会出现这样的问题

vitosubXX commented 7 years ago

@zvving 👍

vitosubXX commented 7 years ago

我测试的计算是正确的, 但是仅限第一次计算, 后面会莫名的进行多次计算, 而后来的计算结果是错误的并且还缓存了 T T

zvving commented 7 years ago

@liuquanwei

理论上 xib 中 cell 宽度与运行时 cell 宽度不一致都可能出现这个问题。 解决你可以试试方法二

zvving commented 7 years ago

@vitosubXX

是这样。第一次对是因为还没来得及调用 systemLayoutSizeFittingSize,系统不会加宽高约束。后续的多了宽度约束,所以都错了

vitosubXX commented 7 years ago

@zvving thx! 我尝试了你给出的 方法一+方法二, 仍然无法解决问题. 这个width让我猝不及防呀 ~ .~

zvving commented 7 years ago

sorry 未能解决你的问题,可能场景不同没考虑全。

重点在于库代码中的 systemLayoutSizeFittingSize 这一行前后,你可以追进去断点看看 content view 的约束,如果有错误的宽度约束想办法干掉试试

SAGESSE-CN commented 7 years ago

试一下把他改成:

        NSLayoutConstraint *widthFenceConstraint = [NSLayoutConstraint constraintWithItem:cell.contentView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:contentViewWidth];
        [cell addConstraint:widthFenceConstraint];

        // Auto layout engine does its math
        fittingHeight = [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
        [cell removeConstraint:widthFenceConstraint];
hoolang commented 7 years ago

我 pod 'UITableView+FDTemplateLayoutCell' 下载的代码还是1.4版本的?

y0unghe commented 7 years ago

@zvving 方法二确实可以。不过我没有用这个库。我参照这个做的:https://mkswap.net/m/ios/2015/07/08/uitableviewcells-with-dynamic-height.html 感觉原理和这个类似啊。连UITableViewAutomaticDimension都不行了。

xhp281 commented 7 years ago

masony 的怎么用啊

changyiyao commented 7 years ago

@y0unghe 我这边对iOS8及以上系统使用UITableViewAutomaticDimension,另外必须结合estimatedHeight(随便设置个正数即可)才可以正确计算高度,否则会出现系统约束冲突(如contentView.height=44)。iOS7用这个fdtemplatecell做兼容。

y0unghe commented 7 years ago

@changyiyao 我已经抛弃这个库了。UITableViewAutomaticDimension能计算正确的高度。这个库反而不行。