在IB中使用UITableViewCell的子类 - 为什么需要重写drawRect:?
关于子类化 UITableViewCell
,上面的链接讨论了需要重写 drawRect:
来显式绘制单元格内容。
但为什么这是必要的呢?我们能不能只在IB中创建UITableViewCell
的子类,在IB中添加所需的子视图,创建所需的插座连接,然后以编程方式设置这些子视图的值。
为什么需要重写drawRect:
?
In relation to subclassing UITableViewCell
, the above link talks about the need to override drawRect:
to explicitly draw the cells content.
But why is this necessary? Can we not just create a subclass of UITableViewCell
in IB, add the required subviews in IB, create the required outlet connections and then set the values of these subviews programmatically.
Why is there a need to override drawRect:
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要重写drawRect:,就像您不需要使用UIAlertView 的textField 实现方法一样。然而,在某些情况下,绘制单元格的内容可以提高性能,特别是如果您有多个非不透明图层并且可以将它们绘制到一个不透明单元格中,因为渲染器不必多次合成和混合。通过绘制到单个不透明层中,硬件只需在其显示在屏幕上之前进行混合即可。
请注意,如果您没有遇到延迟或者每个单元格本身都有动画,那么绘图将不会对您有帮助。
You're not required to override drawRect: any more than you are required to use the textField implementation methods for UIAlertView. Drawing the cell's content, however, can boost performance in some cases, particularly if you have multiple non-opaque layers and can draw them into one opaque cell, because the renderer doesn't have to composite and blend multiple times. By drawing into a single opaque layer, the hardware only has to blend right before it gets displayed on screen.
Note that if you don't experience lag or if you have animation in each cell themselves, then drawing will not help you.
我在你的链接中找不到它说你应该子类化 DrawRect 方法。
我不认为苹果推荐它。
我开发了一个完整的应用程序,其中包含许多带有自定义 tableViewCells 的 tableController,使用 nib 文件,并且从不子类化 drawRect 方法。
我从未注意到任何问题,并且我的应用程序成功通过了应用程序商店审核。
I can't find in your link where it says you are supposed to subclass DrawRect method.
I don't think apple recommand it.
I have developped an entire app with a lot of tableControllers with custom tableViewCells, using nib files and never subclassing drawRect method.
I never noticed any problem, and my app passed successfully the app store review.