epreston / PSTreeGraph

A tree graph view control for iPad applications. This is a port of the sample code from Max OS X to iOS (iPad).
http://epreston.github.io/PSTreeGraph/
259 stars 50 forks source link

How can I represent an array of strings in PSTreeGraph ? #24

Open harikrishnanp opened 10 years ago

harikrishnanp commented 10 years ago

I need to represent an array of strings in tree like structure. How can I do this with PSTreeGraph? I couldn't find any function for adding arbitrary nodes. Please explain.

epreston commented 10 years ago

I need to represent an array of strings in tree like structure. How can I do this with PSTreeGraph? I couldn't find any function for adding arbitrary nodes. Please explain.

You can use anything as a data source so long as it supports the simple protocol. The code in the example has a class that queries the class hierarchy to implement the two required methods. If I was working with an array of strings, I'd try two things: create a wrapper object that implemented the protocol, or add a category to my string array.

As long as your data source understands the hierarchy and can be told where to start, everything else is automatic.

I really learned a lot porting this code from the OSX example. I liked its design in this regard.

We never need to use methods to add and remove nodes, just make the data source smart enough to describe itself. When the data source changes, the graph can be told to update itself. The important point in this design is that your GUI and controller code can focus on updating your data model, without ever worrying about the display.

I think representing nested collections / arrays of strings would be a good example for the control. Something that displays the contents of a plist, would be good.

epreston commented 10 years ago

Thanks for your reply. I couldn't compete understand you. Now the code works in class level hierarchy. Is it possible to shift it to object level?

My original requirement is I have nested NSMutableDictionary. Each Dictionary contains two values. it's name and another dictionary which holds collection of it's children. How can I represent this ?

To get things up and running we have two steps:

Specificly, you'll need to implement the "PSTreeGraphModelNode" protocol in your data object (you could add it as a category on NSMutableDictionary) or create a wrapper object that understands your data structure and can implement the two methods. You can find this defined and documented in "PSTreeGraphModelNode.h"

For your data structure of nested dictionaries, implementing the "childModelNodes" function should be easy because the dictionary exposes methods that return NSArrays. Remember to check the API documentation because the protocol requires an empty array, not nil. You may need to add a quick check.

To customize the display of the the nodes, take a look at how the PSTreeGraphDelegate works. This function is called to populate a view with the data of an object in your data model. It will give you the reference to the view and data model object that are being matched up. This can be as simple or as complex as you need.

The example uses sub-classes to keep things clean, separating library code from the customizations needed by the project. Creating custom node displays can be done programmatically or through interface builder. You can further customize the appearance by setting properties such as orientation and color.