如何使用 Quartz 在 UIView 上绘制子视图
我正在 iPad 应用程序的 UIView 上生成并显示树结构。我们将该视图称为 ROOTVIEW。所有树节点也是 UIView,其中有 label 和 UIImage 作为子视图。当从数据源生成节点时,它将为该节点生成一个 UIView 并添加到 ROOTVIEW 作为子视图。但问题是,当树结构很大时,比如数千个节点,它会使用太多的内存。
因为添加到ROOTVIEW后节点的视图不会改变,所以我想出了一个解决方案:当我们获取节点的UIView时,不是将其作为子视图添加到ROOTVIEW,而是将其绘制在ROOTVIEW上,并释放节点的UIView。所以最后只有一个视图:ROOTVIEW。
我对 Quartz 不太熟悉,如何在 UIView 上绘制子视图?谢谢。
I'm generating and displaying a tree structure on a UIView in an iPad app. let's call the view ROOTVIEW. All the tree nodes are UIViews too, which has label and UIImage as subviews. When a node is generated from the data source, it will generate a UIView for the node and add to ROOTVIEW as subview. But the problem is, when the tree structure is large, like thousands of nodes, it will use way too much memory.
Because the node's view won't change after added to ROOTVIEW, I come up with a solution: when we get the node's UIView, instead of adding it to ROOTVIEW as subview, we draw it on the ROOTVIEW, and release the node's UIView. so at the end there will be only one view: ROOTVIEW.
I'm not quite familiar with Quartz, how can I draw a subview onto UIView? thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,这是正确的方法。只需在根图像视图上渲染单个视图甚至视图树的一部分即可。
请参阅此处如何执行此操作: 将位图表示从一个视图复制到另一个视图的简单或有效的方法?
Yes, it is right way. Just render individual views or even parts of your views tree on root image view.
see how to do this here: Easy or efficient way to copy the bitmap representation from one view to another?
您是否考虑过使用
UITableView
?我知道它不直接支持嵌套节点,但您仍然最好利用 UITableViewController 的功能,而不是实现完整的树组件。您可以尝试将单元格自定义为缩进,以实现树状视觉效果。HTH,
阿克谢
Have you considered using
UITableView
? I know that it doesn't directly supported nested nodes, but you will still be better off utilizingUITableViewController
's functionality, rather than implementing a full fledged tree component. You can try to customize the cells to look indented to achieve a tree-like visual.HTH,
Akshay
除了使用 UITableView 的建议之外,您还可以考虑使用您自己的树节点视图的缓存。这意味着您持有树节点视图的数组,并回收它们 - 每当用户滚动时,都会显示不同的数据,但会显示(几乎)相同数量的树节点。只需分配足够的树节点来填充可见区域,也许再分配两到三个用于滚动边缘,并且每次用户滚动时,更新每个树节点中的数据。 UITableview 中使用了相同的技术,它保存可显示数量的单元格(通常为 14-20,取决于单元格高度),UITalbleView 不会为表中的行数分配单元格(这可能是数百,或数千!)
In addition to the advice of using UITableView, you might also consider using a cache of your own tree-node views. This means that you hold an array of tree-node views, and recycle them - whenever the user scrolls, different data is displayed, but (almost) the same amount of tree nodes is displayed. Just allocate enough tree nodes to fill the visible area, maybe with two-three more for scrolling edges, and every time the user scrolls, update the data in each tree-node. The same technique is used in the UITableview, where it holds cells in the amount that can be displayed (usually 14-20, depending on cell height), The UITalbleView does NOT allocate cells for the amount of rows in the table (which might be hundreds, or thousands!)