【问题标题】:UITableView jumps when expanding cell with autolayout. [duplicate]使用自动布局扩展单元格时 UITableView 会跳转。 [复制]
【发布时间】:2016-03-23 20:52:55
【问题描述】:

我一直在寻找,但找不到解决此问题的方法。我当前的代码基于Reload section without reloading section header

当用户按下单元格时,我正在尝试增加一个 uitableview 单元格。我有一个 UITableViewCell 子类,其 UILabel 最初仅限于两行文本。当用户按下 UILabel 时,我将 UILabel 中的 numberOfLines 属性设置为 0,以便它根据需要扩展到尽可能多的行。

这是我在 tableView 中的代码:didSelectRowAtIndexPath:

if (((NSNumber *)expandedCells[indexPath.section]).boolValue)
{
     labelExpandingCell.cellLabel.numberOfLines = 10;
} else
{
     labelExpandingCell.cellLabel.numberOfLines = 3;
}
[UIView setAnimationsEnabled:NO];
[self.tableView beginUpdates];
[self.tableView setNeedsDisplay];
[self.tableView endUpdates];
[UIView setAnimationsEnabled:YES];

有时这可以正常工作,通常在 UITableView 的 contentOffset.y == 0 时。通常当我执行此代码时,tableview 会向后跳转 100 像素左右。这是运行此代码之前和之后的示例日志。

2016-03-25 14:57:08.549 TableViewTest[485:129926] 更新前的内容偏移量:153.500000 2016-03-25 14:57:08.568 TableViewTest[485:129926] 更新后的内容偏移量:136.500000

当我展开当前可见的单元格时,为什么自动布局会导致我的表格视图向后跳转?

【问题讨论】:

  • 我得到了与以前可以工作的代码类似的东西。不确定这是什么时候开始的。 iOS 9 sdk 可能吗?当然不想回到手动设置单元高度!

标签: ios objective-c uitableview uiscrollview contentoffset


【解决方案1】:

我通过在我的 tableview 的委托中实现estimatedHeightForRowAtIndexPath 方法解决了这个问题。我认为问题在于 tableview 在没有实现此方法的情况下无法正确估计我的 tableview 中前面部分的高度,因此当我更新 tableview 中的部分时会跳转。

【讨论】:

  • 是的,我通过添加固定高度和删除estimateHeight.. :) 解决了问题
【解决方案2】:

你设置了 UITableView

tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = DEFAULT_ROW_HEIGHT

如果是,请检查确保您的 UILabel 约束已正确应用于单元格。 如果设置了以上两个属性,就可以避免实现“estimatedHeightForRowAtIndexPath”方法。

【讨论】:

  • 它不适合我!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-22
  • 2012-01-04
  • 2015-05-03
  • 1970-01-01
  • 2016-08-19
  • 2011-10-27
  • 1970-01-01
相关资源
最近更新 更多