Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Tree like strucure in iPhone

I need to represent my data like a Tree structure in UI.How can I create a view like this in iPhone? Is there any default items for this in object library ? If no how can I manually implement this?

enter image description here

like image 501
Harikrishnan Avatar asked Nov 06 '25 04:11

Harikrishnan


2 Answers

There is in Core Foundation something like CFTree

But, to be honest, I wouldn't use it, because it is too low level and it's in pure C. It would be better to implement tree structure by your own.

You can achieve it in many different ways, depending on what kind of tree you want to have. Here is an example of the easiest interface:

@interface XYNode : NSObject
  @property(nonatomic, readonly) YourDataObjectType *data;
  @property(nonatomic, readonly) NSSet *children;
  @property(nonatomic, readonly) XYNode *parent;

  + (instancetype)nodeWithParent:(XYNode *)parent data:(YourDataObjectType *)data;
  - (instancetype)initWithParent:(XYNode *)parent data:(YourDataObjectType *)data;

  - (void)addChildNode:(id)node;
  - (void)removeChildNode:(id)node;
@end
like image 145
Maciej Oczko Avatar answered Nov 08 '25 11:11

Maciej Oczko


There is a project on github called PSTreeGraph made by Ed Preston

https://github.com/epreston/PSTreeGraph

Author says that it is for iPad but work also on iPhone, simply by changing the device to iPhone in xcode "deployment info"

In addition, it's interactive.

EDIT

Possibility to add node "manually":

In PSHTreeGraphViewController.m

you have an entry point [self setRootClassName:@"UIControl"];

here you can check how to create the tree.

That project also gives you data model : ObjCClassWrapper

And finally there is a method (delegate)

-(void) configureNodeView:(UIView *)nodeView
            withModelNode:(id <PSTreeGraphModelNode> )modelNode

called on a node tap.

Everything to create a custom interactive tree!

like image 45
Alban Avatar answered Nov 08 '25 11:11

Alban