Rolling your own iPhone treeview control Part 1: Creating a tree node class

So here's a challenge I wasn't able to find any code online for -- creating a treeview control on iOS. It sounds so fundamental that you'd think somebody would've gotten around to doing it but -- no such luck.

Well, if you're looking for one, here's how I did mine.

I wanted to create a simple test case to do this, so I'm going to use the following mock tree as a guideline:

Root
>Node1
  >Node1a
    >Node1a1
  >Node1b
    >Node1b1
    >Node1b2
    >Node1b3
>Node2
  >Node2a
    >Node2a1

First let's go back to some POOCOs (Plain old Objective-C objects) - a tree structure. We need a class that holds an index, some sort of value, as well as a reference to its parent and an array or collection to hold its children nodes. Let's call this class MyTreeNode.

In its simplest MyTreeNode.h will look like this:

#import <Foundation/Foundation.h>

@interface MyTreeNode : NSObject {
    MyTreeNode *parent;
    NSMutableArray *children;
    int index;
    NSString *value;
}

@property (nonatomic, retain) MyTreeNode *parent;
@property (nonatomic, retain, readonly) NSMutableArray *children;
@property (nonatomic) int index;
@property (nonatomic, retain) NSString *value;

@end

...while the implementation on MyTreeNode.m looks like this:

#import "MyTreeNode.h"

@implementation

@synthesize parent, children;
@synthesize index, value;

@end

Now, note that the children attribute is of type NSMutableArray, which isn't strongly typed. To make it easier and to enforce some type-safety on the list, we'll add a method named addChild, with signature:

- (void)addChild:(MyTreeNode *)newChild;

and implementation:

- (void)addChild:(MyTreeNode *)newChild {
	newChild.parent = self;
	[self.children addObject:newChild];
}

The addChild method does two things: it adds a MyTreeNode object to the children attribute as expected, but it also sets the parent attribute of the child to the current object.

In the next post we'll figure out what basic iPhone UIControls we can use for implementing our treeview, and what else we'll need to do for MyTreeNode to work.

About Jon Limjap

Jon Limjap has been programming since he was 12 and hasn't stopped yet. He was gone for a while in iOS and Java land, but is now back in .NET searching for unicorns and hunting down dragons.
This entry was posted in Technical Articles and tagged , , . Bookmark the permalink.

9 Responses to Rolling your own iPhone treeview control Part 1: Creating a tree node class

  1. bonskijr says:

    si CS iOs version ba to?

  2. Pingback: Rolling your own iPhone treeview control Part 2: Responding to the UITableViewDataSource protocol « .NET @ Kape ni LaTtEX

  3. Pingback: Rolling your own iPhone treeview control Part 3: Creating a treeview-ready UITableViewCell « .NET @ Kape ni LaTtEX

  4. Pingback: Rolling your own iPhone treeview control Part 4: Putting it all together in a UITableView and sorting out performance kinks | .NET @ Kape ni LaTtEX

  5. James says:

    I am currently trying to develop an automatic puzzle solver for my iPhone app, and it seems that TreeView may be the way forward. However, I’d really appreciate your opinion on this because it is all quite new to me!

    My game involves moving objects around a series of tiles, so for each position I will establish which direction the player can move, and then move in these directions, creating branches as I go. Then hopefully one of the branches will be a solution to the puzzle. Is using TreeView a good way to do this? I have no idea – it’s completely new to me.
    Thanks in advance!

  6. Jon Limjap says:

    James,

    I’m not really sure it’s applicable… I suspect not. The paradigm for any tiled game or such would be blocked cells and I don’t think the tree paradigm fits in well to that. You’ll have to probably roll your own code for that.

  7. bimal says:

    hello all,
    once i add all data and if i want to flush all data after goes to next site… from where i can remove all data ……

    thanks for your help in advance
    bimal

  8. Pingback: Rolling your own iPhone treeview control: Summary, conclusion, and download links | Coding @ Kape ni LaTtEX

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>