iPhone UITableViewCell 添加多个视图与使用drawRect 的一个视图的性能比较
我正在创建一些 UITableViewCells
,其中包含多个自定义项目,我的问题是要走什么路线:
1)将我需要的所有内容作为单独的视图添加到 contentView,使用 UILabels
作为文本和用于图像的 UIImageViews
(当前使用此)
或
2) 将单个子类视图添加到 contentView,并将所有文本作为 NSString
然后是我的图像和 UIImages
并绘制 根据
我认为,除非您实际上在 contentView 中绘制线条或其他内容,否则简单地使用选项 1 会更容易,并且 对此 Cocoa with love 自定义单元 它效率更高,尽管它是有点老了,不知道现在有没有变化。
那么什么对性能更好,在什么情况下你肯定会使用其中一种而不是另一种呢?
I am creating some UITableViewCells
with several custom items inside, my question is what route to go:
1) Add all I need as separate views to contentView, using UILabels
for text and UIImageViews
for images (currently using this)
or
2) Add a single subclassed View to contentView, and all my text as NSString
then my Images and UIImages
and draw everything at drawRect
What would perform better, and in what scenarios would one be the better choice over the other?, as I see it unless you are actually drawing lines or something in the contentView, simply going with option 1 would be easier, and according to this Cocoa with love custom cells it is more efficient although it is a bit old, so I don't know if this has changed.
So what is better for performance and in which situation would you definitely use one over the other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从使用单独的 UILabel 和 UIImageView 的第一种方法开始。在实际设备上进行测试并在发现性能问题时进行优化。
根据内容,我选择的选项 1 比在一个视图中绘制所有内容更快。特别是当该视图必须绘制图像时。 UIImageView 经过苹果的大力优化。
获取性能指标的一个好方法是 Core Animation 工具。
Start with the first method of using separate UILabel's and UIImageView's. Test on actual devices and optimize if you see performance problems.
Depending on the content I've had option 1 be faster than drawing everything in one view. Especially when that view had to draw images. UIImageView's are heavily optimized by apple.
A good way to get metrics on performance is the Core Animation instrument.
我认为自己绘制文本会稍微更有效率。但是,只要您保持所有标签的背景不透明,我怀疑差异是否足以产生影响,因此请选择最容易编程的内容,并避免过早优化。
(不透明背景的事情非常重要 - 具有透明背景的标签是表视图效率低下的一个重要原因。即便如此,除非您有数百个,否则可能不会有太大问题。)
I think drawing the text yourself would be marginally more efficient. However, as long as you keep the backgrounds of all your labels opaque, I doubt the difference would be enough to matter, so go with whatever is easiest to program, and avoid premature optimisation.
(The opaque backgrounds thing is quite important — labels with transparent backgrounds are a big cause of inefficiency in table views. Even so, unless you have hundreds it probably won't be much of a problem.)