The MAF tree view component works with the hierarchical data structure defined by the MAF. This data structure is called MAFTreeContent, and it holds MAFTreeDefaultItem typed objects that can either represent leaf (also known as child) elements or compound/parent elements that hold one or more child elements.
@interface MAFTreeContent : NSObject <MAFTreeContentProvider> @property (nonatomic, retain, readonly) NSMutableDictionary* content; @property (nonatomic, retain, readonly) MAFTreeItemDefault* root; - (id) initWithRootLabel:(NSString*)label_in; - (MAFTreeItemDefault*) addChildItemToParent:(MAFTreeItemDefault*)parent_in withId:(NSString*)id_in andLabel:(NSString*)label_in; - (MAFTreeItemDefault*) addChildItemToParent:(MAFTreeItemDefault*)parent_in withId:(NSString*)id_in andLabel:(NSString*)label_in andImage:(UIImage*)image_in; - (void) removeChildrenFromParent:(MAFTreeItemDefault*)parent_in items:(NSArray*)items; @end
To create the invisible root element, use the initWithRootLabel method of MAFTreeContent. Use the root property of the class to get access to the root MAFTreeDefaultItem and start adding (creating and linking) subelements to it.
- (MAFTreeContent*) createContent { // create root item, which is not visible, but holds the whole tree MAFTreeContent* mafTreeContent = [[[MAFTreeContent alloc] initWithRootLabel:@"root"] autorelease]; // add "leaves" files to root [mafTreeContent addChildItemToParent:mafTreeContent.root withId:@"5" andLabel:@"mach_kernel"]; [mafTreeContent addChildItemToParent:mafTreeContent.root withId:@"6" andLabel:@"mach_kernel2"]; [mafTreeContent addChildItemToParent:mafTreeContent.root withId:@"7" andLabel:@"mach_kernel3"]; // ... // create folder "bin" and bind under root MAFTreeItemDefault* mafTreeItem1 = [mafTreeContent addChildItemToParent:mafTreeContent.root withId:@"1" andLabel:@"bin" andImage:[UIImage imageNamed:@"OceanBlueDesktop.png"]]; // add "leaves" / files into the "bin" folder [mafTreeContent addChildItemToParent:mafTreeItem1 withId:@"11" andLabel:@"kill"]; [mafTreeContent addChildItemToParent:mafTreeItem1 withId:@"12" andLabel:@"mkdir"]; [mafTreeContent addChildItemToParent:mafTreeItem1 withId:@"13" andLabel:@"mv"]; [mafTreeContent addChildItemToParent:mafTreeItem1 withId:@"14" andLabel:@"rm"]; [mafTreeContent addChildItemToParent:mafTreeItem1 withId:@"15" andLabel:@"sh"]; // ... // add more items, sub items to any referenced items, and build the tree // ... // return the tree, referenced by its root element return mafTreeContent; }
This example, after creating the root MAFTreeContent item, adds three leaf elements and links them to the root of the tree. It does not use the return values of the addChildItemToParent method, because the concrete leaf objects are not yet important.
Next, it creates a parent item that holds additional subelements, and links that item to the root. To add more child elements to it, this parent item’s reference is stored in the mafTreeItem1 variable, and the next five lines of code, attaches additional child elements to mafTreeItem1 as the parent.
To use an existing hierarchical data structure, for example, a file system, use recursive traversal of the data structure, and map its data and links to the MAFTreeContent and MAFTreeDefaultItem objects.
The order in which you add elements to the tree/MAFTreeContent is important. In the visualized tree view, when building MAFTreeContent, elements appear in the same order in which you add them. To show the elements representing folders first, followed by the leaf “file” elements, add and link the objects representing the folders first.
For a complete list of the MAFTreeContent/MAFTreeDefaultItem/MAFTreeItem protocol members, see their respective public header files.