PSTreeGraph
PSTreeGraph copied to clipboard
How to add new objects to tree and refresh
Absolutely fantastic control! Forgive me for creating an issue, as I just really want to ask you a question.
How do I add a new item to the tree and refresh the tree to show the new item while the app is running.
-
(void)addButtonTapped:(id)sender { NSLog(@"addButtonTapped model root %@", treeGraphView.modelRoot);
EntityModel *rootEntity = (EntityModel *)treeGraphView.modelRoot; EntityModel *newEntityModel = [[EntityModel alloc]initWithParentNode:rootEntity];
[rootEntity addChildModelNode:newEntityModel]; [newEntityModel release]; }
Thank you for the feedback. :-)
I think I'll join you on this one. :-) That's the perfect scope for exercising this thing. In the process, we'll create some more examples, review the control, and add more documentation to the wiki.
Until I get a few hours to cut some code and markup some thoughts, start with this;
Quick Tour:
This sample is using the MVC (model view controller) pattern. The view part is the control, the model part is some clever class that gets information from the objective-c runtime on registered classes, and the controller part is a UIViewController. The controller is the glue between them that knows about both.
Important to Know:
- The model data has no idea on how or if it will ever be displayed. It's just the data structure with two extra methods to describe itself a little more.
- This view control is easier than most in that you speak to it in terms of your data model. Want to select a node? Give it the data model node you want selected and it is done. It knows nothing about your data except what it has used on screen to match up with each data model node.
- The controller knows about your data model and your customised leaf view. The view control will ask the controller to play match maker during " configureNodeView: withModelNode: " requests as needed. This controller is probably driving most of your application and interacting with many other controls. (or swapping places with other controllers screens of content change).
Where to start:
In short, you want to swap out data models, customise the leaf views, add some code to modify data model nodes.
The data model is simple, just implement the two methods of the protocol. The rest should be done in a sub class like PSHLeafView. This is an example subclass that has an expand collapse button. Its best practice to create a subclass like it and make your changes there. In the example, you can change the leaf views appearance quickly by editing ObjCClassTreeNodeView. Modifying the PSHTreeGraphViewController will tie your model and display together.
We'll have to put some thought into how we implement the add / edit.
Shopping List:
- Duplicate the "Example 1" project in the folder.
- Rename the copy, then open up the project file inside to rename the project in Xcode.
- Ensure everything builds, then create a class hierarchy for the data model. (single parent, multi child)
- Add the node protocol to your the objects of your data model.
- Create another xib file like ObjCClassTreeNodeView, set its' custom class on the main view to PSHLeafView.
- Set the "files owner" custom class to PSBaseSubtreeView
- Change the appearance but keep it small.
- Use UITextView's instead of UILabels for text. We can use them for editing later.
- Set boarder to none and uncheck enabled on the text fields posing as labels.
- Find a place for an "Add" button on the node view.
- Customize PSHLeafView with additional code to support your view. (keep the expand button)
- Wire up the IBOutlets and IBActions, make sure you kept the expand button.
- Modify PSHTreeGraphViewController to use your data model objects and custom xib file.
- Change the call to [treeGraphView setModelRoot: (your object model root node here)]
- Modify the delegate method configureNodeView: withModelNode: to populate the display for each node when requested.
- Populate the method to add nodes to the data model.
- Decide on an interaction method for editing text labels. I like the idea of doing it place as the new node appears.
- Set the TreeGraphs custom input view to "nil" after the view has been loaded so the default keyboard will appear when editing text.
- Implement the (name, description etc) update logic in PSHLeafView
- Code, test, commit, review, commit, code, test...
It is much easier to follow the conversation if you post responses into new comments instead of editing the original post. Cheers :-)
Hi there, apologies for modifying the original post won't happen again.
Cheers for helping me with this it will be fantastic if we can get this working.
So far I have created my own data model 'EntityModel' and created the add button and managed to get it loading with dummy items (which will eventually come for a data source) etc but the main thing I am totally unsure about how to get going is basically adding new items and refreshing.
My requirements for the control have changed slightly in that I am picturing this scenario:
"The control loads and there is a single node visible. The user can tap the add button and create any number of sub nodes. The user can then tap on a particular node and a popovercontroller appears and allows the user to edit the title, description, type and any other fields as well. When the popovercontroller disappears it updates the node."
How are you picturing actually refreshing a node/the control to show new items?
Hi Ed, do you perhaps have a few pointers or ideas to get be started on the adding/refreshing? Really want to use this control but not sure where to go from here.
Cheers,
Hi Ed,
Just to let you know I implemented refresh but I am sure you will think its a serious hack but at this stage it works and is the only way I could think of doing it.
Basically when you call SetRootNode instead of passing it the root node, it asks the delegate for the rootnode. This way I can call this method anytime I need a refresh.
Works okay except when there many shapes on the view then it looks a bit dodgy.
Looking forward to hearing your ideas :)
I also needed a control that allowed on the fly refreshing. I created a modified version that allows one to add new nodes to the tree and refresh the tree. The modified version also allows one to create the node view's without using nibs, just to make it a little different. Check out https://github.com/evanlucas/TreeNodeControl. Thanks for the work you did to port this over to iOS.
Your welcome. I agree it really should support a dynamic refresh. In addition, I think it should support multiple animation styles. I plan to have a tilt at it in an effort to modernise the objective-c, reduce the size of the code base, and add more features.