IOS中如何提高UITableView的性能?

最近看了一篇关于如何提高UITableView性能的文章,英文原版的地址:https://medium.com/ios-os-x-development/perfect-smooth-scrolling-in-uitableviews-fd609d5275a5#.jet1gcbax,看完之后将重要的内容总结如下:

1,对于:

tableView:cellForRowAtIndexPath:

Don’t perform data binding at this point, because there’s no cell on screen yet. For this you can use tableView:willDisplayCell:forRowAtIndexPath:method which can be implemented in the delegate of UITableView. The method called exactly before showing cell in UITableView’s bounds

不要在这个方法里面给cell赋值,因为这个时候Cell还没有出现。对此,你可以在tableView:willDisplayCell:forRowAtIndexPath:中进行,这同样是UITableView的一个代理方法,这个方法在Cell即将在UITableView的范围中显示的时候调用。

2. 在返回Cell的高度时候

I recommend not using even complex math calculations on your way to define further height of a cell, only addition, subtraction, multiplication and division (if possible).

在决定即将显示的Cell的高度的时候,建议不要使用复杂的计算,如果可能的话仅用加减乘除;

3.用AutoLayout自动适配Cell的高度怎样?

如果子视图少的话是可以的,如果子视图多的话,使用AutoLayout,性能也会减少很多的:And more subviews you have, less quickly AutoLayout works.

4. Perform code optimizations to achieve balance of loading CPU & GPU. You should clearly know which part of rendering must be done by GPU, and which one — by CPU for keeping balance.

 将代码最优化,以达到CPU和GPU使用的平衡,你应该提前知道哪里一定要使用GPU进行渲染,哪里使用CPU来保持二者的平衡;

5. We can perform rendering at CPU by using CoreGraphics operations in drawRect: method of UIView by this way:

我们可以选择CoreGraphics利用CPU在drawRect方法中,进行渲染,实例代码如下所示

原文地址:https://www.cnblogs.com/Mike-Fighting/p/5068654.html