4

我一直在寻找,找不到解决这个问题的方法。我当前的代码基于Reload section 而没有重新加载 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

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

4

2 回答 2

6

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

于 2016-03-31T20:13:48.150 回答
1

你设置了 UITableView

tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = DEFAULT_ROW_HEIGHT

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

于 2016-07-27T04:37:24.173 回答