以编程方式创建UITableViewCell或从nib加载一个更快? [英] Is it faster to create UITableViewCell programmatically or load one from a nib?

查看:134
本文介绍了以编程方式创建UITableViewCell或从nib加载一个更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时间分析器显示我的应用程序中最耗时的操作是从nib文件加载 UITableViewCells 。最昂贵的是用4KB图像加载 UITableViewCell

Time profiler shows the most time consuming operation in my app is loading UITableViewCells from nib files. The most expensive of which involves loading a UITableViewCell with a 4KB image.

我加载 UITableViewCell 使用以下代码:

    [[NSBundle mainBundle] loadNibNamed:@"UITableViewCellPortrait" owner:self options:NULL];
    cell = portraitCell;
    self.portraitCell = nil;

有人比较了以编程方式创建视图或加载 UITableViewCell 来自nib?

Has anyone compared the difference between creating a view programmatically or loading a UITableViewCell from a nib?

编辑:

我比较了重复运行加载 UITableViewCell ,并以编程方式创建视图。我的测试涉及在3-5秒的时间段内两个 UITableViews 之间交替大约10次。在每个测试中,以编程方式加载 UITableViewCell 的速度快了2到6倍。


I compared the time profile of repeated runs of loading the UITableViewCell from a nib and creating the view programmatically. My test involved alternating between two UITableViews about 10 times in the span of 3-5 seconds. In each test, loading the UITableViewCell programmatically was substantially faster, between 2x to 6x faster.

任何人都可以证实这些结果?

Can anyone corroborate these results?

编辑:
我更新了nib加载代码,只加载一次nib文件,并使用缓存版本进行后续调用。

I updated the nib loading code to only load the nib file once and use a cached version for subsequent calls.

    if (self.UITableViewPortaitNib == nil) {
        self.UITableViewPortaitNib = [UINib nibWithNibName:@"UITableViewCellPortrait" bundle:[NSBundle mainBundle]];
    }

    self.UITableViewPortaitNib instantiateWithOwner:self options:NULL];
    cell = portraitCell;
    self.portraitCell = nil;

我也使用自动化仪器创建更一致的运行,结果仍然建议加载 UITableViewCells 以编程方式比为一个nib加载 UITableViewCells 更快。从nib加载 UITableViewCells 的平均运行时间约为90ms,而以编程方式创建 UITableViewCell 的平均运行时间

I also used the automation instrument to create more consistent runs and the results still suggest loading UITableViewCells programmatically is faster than loading UITableViewCells for a nib. The average running time for loading UITableViewCells from a nib was around 90ms, while the average running time for creating the UITableViewCell programmatically was 50ms.

推荐答案

尝试创建一个 UINib 对象,然后发送它 instantiateWithOwner:options:每次需要创建一个新的单元格。从 UINib类别参考

Try creating a UINib object once and then sending it instantiateWithOwner:options: each time you need to create a new cell. From the UINib Class Reference:


例如,如果表视图使用nib文件实例化表视图单元格,将nib缓存在 UINib 对象可以显着提高性能。

For example, if your table view uses a nib file to instantiate table view cells, caching the nib in a UINib object can provide a significant performance improvement.

这篇关于以编程方式创建UITableViewCell或从nib加载一个更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆