How to draw a subview onto UIView using Quartz
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 ch开发者_如何转开发ange 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.
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?
Have you considered using UITableView
? I know that it doesn't directly supported nested nodes, but you will still be better off utilizing UITableViewController
'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
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!)
精彩评论