我在将 Autolayout 与动态单元格调整大小结合起来并在选择时让单元格增长时遇到问题。
我当前的实现在未选中状态下将单元格设置为高度 70。它使用 AutoLayout 进行布局,并隐藏了额外的信息,从 71 到 220 像素:
Normal cell label
---------------- 70px
UILabel
UITextView
UIButton
---------------- 220px
在这种情况下,展开的信息使用 AutoLayout 定位在普通单元格标签下方。
扩展信息中的 UITextView 应该增长以适应内容,将单元格的大小增加到 220 像素以上。
我在 heightForRow 中计算这个值,方法是让一个单元格出列、配置它并使用 systemLayoutSizeFittingSize: 询问它的高度。
不幸的是,这在大约 75% 的情况下无法给我正确的尺寸。异常(exception)情况是:
1. I load the normal cell and tap to expand.
2. I then scroll down, and scroll back up to the expanded cell.
3. The expanded cell is now the correct height after recycling (still expanded)
我听说 systemLayoutSizeFittingSize: 需要从上到下的 AutoLayout 约束,但我不能强制执行底部约束,因为这意味着这些元素在未展开状态下是可见的。
问题似乎源于 AutoLayout,事实上它在大多数情况下都不会产生正确的尺寸,即使使用 setNeedsLayout 和 layoutIfNeeded 也是如此。
大多数情况下,我一直在使用以下教程:http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout
最佳答案
你累了吗
[tableView reloadData]
或者
[tableView reloadRowsAtIndexPaths:<#(NSArray *)#> withRowAnimation:<#(UITableViewRowAnimation)#>]
单元格需要重新布局,因此当您向下滚动和向后滚动时,单元格会被回收并在重新布局时正确确定其高度。
关于ios - 使用自动布局、动态单元格大小和扩展单元格正确调整 UITableViewCell 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28076348/