Why would layoutIfNeeded be called upon scrolling the uitableviewcontroller?

时间:2016-04-04 16:49:52

标签: ios performance uitableview uikit instruments

I'm trying to optimize the UITableViewController for slower devices, and after eliminating many of the reasons why a tableview might be slow, I'm still trying to figure out the last one...

enter image description here This screenshot is from instruments, showing the most expensive calls, during a time where no call is made to get heightForRowAtIndexPath or cellForRowAtIndexPath (scrolling ~10px up and down repeatedly without causing any cell to go out of the table, or get rendered).

Is this normal behavior? Why would the view need relayout if the content of the page is not changing?

2 个答案:

答案 0 :(得分:2)

每次滚动时,都会更改contentOffsetcontentOffset更新bounds,特别是origin。每个bounds更改标记接收器,以便在下一个更新周期中进行布局。在这种情况下更新frame浮动子视图,如滚动指示器,表格页眉和页脚,......这是完全正常的行为,你无能为力。别担心,你正在重新显示,而不是重绘。

答案 1 :(得分:1)

我们必须看到一些代码来确定调用该方法的原因,但至少可能是出于以下原因之一:

  1. 即使您没有看到它们,滚动时也会有更改的约束,它们可能会受到影响,可能在UITableViewCell内部?
  2. 它与表视图没有任何关系,视图只有其他一些布局,也许你做了一个约束修改......
  3. 您正在调用一个明确调用它的方法。
  4. 另外请记住,这只会在需要的时候布局视图。