### Install RATreeView with CocoaPods Source: https://github.com/fandongtongxue/ratreeview/blob/master/README.md This code snippet shows how to add the RATreeView library to your Xcode project using CocoaPods. It involves adding a line to your Podfile and then running the 'pod install' command. You will then need to import the library using '#import '. ```ruby pod "RATreeView", "~> 2.1.2" ``` -------------------------------- ### Import RATreeView in Objective-C Source: https://github.com/fandongtongxue/ratreeview/blob/master/README.md Demonstrates how to import the RATreeView library in your Objective-C files. The specific import statement depends on whether you installed RATreeView using CocoaPods or by manually adding the source files to your project. ```objectivec // In case you are using RATreeView with CocoaPods #import ``` ```objectivec // In case you are using RATreeView by simply copying // source files of the RATreeView into your project #import "RATreeView.h" ``` -------------------------------- ### Access Tree View Items and Cells (Objective-C) Source: https://context7.com/fandongtongxue/ratreeview/llms.txt Query the tree view for items, cells, and hierarchy information. This includes methods to get a cell for an item, an item for a cell, items at specific points or within rectangles, and to retrieve parent items and hierarchy levels. ```objc #pragma mark - Accessing Items and Cells // Get cell for an item UITableViewCell *cell = [self.treeView cellForItem:dataObject]; // Get item for a cell id item = [self.treeView itemForCell:cell]; // Get item at a specific point CGPoint point = CGPointMake(100, 200); id itemAtPoint = [self.treeView itemForRowAtPoint:point]; // Get items in a rect CGRect rect = CGRectMake(0, 0, 320, 400); NSArray *itemsInRect = [self.treeView itemsForRowsInRect:rect]; // Get all visible cells and items NSArray *visibleCells = [self.treeView visibleCells]; NSArray *visibleItems = [self.treeView itemsForVisibleRows]; // Get parent of an item id parent = [self.treeView parentForItem:dataObject]; // Get hierarchy level (0 = root level) NSInteger level = [self.treeView levelForCellForItem:dataObject]; NSInteger levelFromCell = [self.treeView levelForCell:cell]; // Check if item is expanded BOOL isExpanded = [self.treeView isCellForItemExpanded:dataObject]; BOOL isCellExpanded = [self.treeView isCellExpanded:cell]; // Get total number of visible rows NSInteger numberOfRows = [self.treeView numberOfRows]; ``` -------------------------------- ### Define Data Model for Tree Structure Source: https://context7.com/fandongtongxue/ratreeview/llms.txt This Objective-C code defines a data model class, `RADataObject`, for representing nodes in a hierarchical tree structure. It includes properties for the node's name and its children, along with methods for initialization, adding, and removing child nodes. The example also shows how to build a sample tree structure. ```objective-c // RADataObject.h #import @interface RADataObject : NSObject @property (strong, nonatomic) NSString *name; @property (strong, nonatomic) NSArray *children; - (id)initWithName:(NSString *)name children:(NSArray *)array; + (id)dataObjectWithName:(NSString *)name children:(NSArray *)children; - (void)addChild:(id)child; - (void)removeChild:(id)child; @end // RADataObject.m @implementation RADataObject - (id)initWithName:(NSString *)name children:(NSArray *)children { self = [super init]; if (self) { self.children = [NSArray arrayWithArray:children]; self.name = name; } return self; } + (id)dataObjectWithName:(NSString *)name children:(NSArray *)children { return [[self alloc] initWithName:name children:children]; } - (void)addChild:(id)child { NSMutableArray *children = [self.children mutableCopy]; [children insertObject:child atIndex:0]; self.children = [children copy]; } - (void)removeChild:(id)child { NSMutableArray *children = [self.children mutableCopy]; [children removeObject:child]; self.children = [children copy]; } @end // Building the tree structure - (void)loadData { RADataObject *phone1 = [RADataObject dataObjectWithName:@"Phone 1" children:nil]; RADataObject *phone2 = [RADataObject dataObjectWithName:@"Phone 2" children:nil]; RADataObject *phones = [RADataObject dataObjectWithName:@"Phones" children:@[phone1, phone2]]; RADataObject *notebook1 = [RADataObject dataObjectWithName:@"Notebook 1" children:nil]; RADataObject *computer1 = [RADataObject dataObjectWithName:@"Computer 1" children:@[notebook1]]; RADataObject *computers = [RADataObject dataObjectWithName:@"Computers" children:@[computer1]]; self.data = @[phones, computers]; } ``` -------------------------------- ### Initialize and Configure RATreeView in Objective-C Source: https://github.com/fandongtongxue/ratreeview/blob/master/README.md This Objective-C code shows the basic steps to initialize and configure a RATreeView. It involves creating an instance of RATreeView, setting its delegate and data source, adding it as a subview, and then reloading its data. ```objectivec RATreeView *treeView = [[RATreeView alloc] initWithFrame:self.view.bounds]; treeView.delegate = self; treeView.dataSource = self; [self.view addSubview:treeView]; [treeView reloadData]; ``` -------------------------------- ### Configure Tree Header/Footer Views in Objective-C Source: https://context7.com/fandongtongxue/ratreeview/llms.txt Demonstrates how to set custom header and footer views for the tree, and register reusable header/footer view classes. This includes creating a header view with a label, setting an empty footer view, and registering both class-based and nib-based header/footer views. ```objc // Set tree-wide header and footer views UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; headerView.backgroundColor = [UIColor blueColor]; UILabel *headerLabel = [[UILabel alloc] initWithFrame:headerView.bounds]; headerLabel.text = @"Tree Header"; headerLabel.textAlignment = NSTextAlignmentCenter; [headerView addSubview:headerLabel]; self.treeView.treeHeaderView = headerView; // Empty footer view to remove extra separators self.treeView.treeFooterView = [UIView new]; // Register header/footer view class (iOS 6+) [self.treeView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"Header"]; [self.treeView registerNib:[UINib nibWithNibName:@"CustomHeader" bundle:nil] forHeaderFooterViewReuseIdentifier:@"CustomHeader"]; // Dequeue reusable header/footer view UITableViewHeaderFooterView *header = [self.treeView dequeueReusableHeaderFooterViewWithIdentifier:@"Header"]; ``` -------------------------------- ### Handling Row Selection and Deselection in RATreeView (Objective-C) Source: https://context7.com/fandongtongxue/ratreeview/llms.txt Explains how to manage row selection and deselection in RATreeView using delegate methods. It covers pre-selection and pre-deselection callbacks, post-selection and post-deselection actions, programmatic selection/deselection, retrieving selected items, and configuring selection behavior. Dependencies include the RATreeView framework and a delegate object conforming to RATreeViewDelegate. ```objc #pragma mark - RATreeViewDelegate - Selection // Called before a row is selected - return item to select or nil to prevent - (id)treeView:(RATreeView *)treeView willSelectRowForItem:(id)item { NSLog(@"Will select: %@", [(RADataObject *)item name]); return item; // Return item to allow selection, nil to prevent } // Called after a row is selected - (void)treeView:(RATreeView *)treeView didSelectRowForItem:(id)item { RADataObject *dataObject = item; NSLog(@"Selected: %@", dataObject.name); // Toggle expansion on selection if ([treeView isCellForItemExpanded:item]) { [treeView collapseRowForItem:item withRowAnimation:RATreeViewRowAnimationFade]; } else { [treeView expandRowForItem:item withRowAnimation:RATreeViewRowAnimationFade]; } } // Called before a row is deselected - (id)treeView:(RATreeView *)treeView willDeselectRowForItem:(id)item { return item; // Return item to allow deselection } // Called after a row is deselected - (void)treeView:(RATreeView *)treeView didDeselectRowForItem:(id)item { NSLog(@"Deselected: %@", [(RADataObject *)item name]); } // Programmatic selection [self.treeView selectRowForItem:dataObject animated:YES scrollPosition:RATreeViewScrollPositionMiddle]; // Programmatic deselection [self.treeView deselectRowForItem:dataObject animated:YES]; // Get currently selected items id selectedItem = [self.treeView itemForSelectedRow]; NSArray *selectedItems = [self.treeView itemsForSelectedRows]; // Selection configuration self.treeView.allowsSelection = YES; self.treeView.allowsMultipleSelection = NO; self.treeView.allowsSelectionDuringEditing = YES; self.treeView.allowsMultipleSelectionDuringEditing = NO; ``` -------------------------------- ### Implement RATreeView Data Source Methods in Objective-C Source: https://github.com/fandongtongxue/ratreeview/blob/master/README.md These Objective-C methods are required for the RATreeView's data source protocol. They define the number of children for a given item, how to retrieve a specific child, and how to create a cell for an item. ```objectivec - (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item { return item ? 3 : 0; } ``` ```objectivec - (UITableViewCell *)treeView:(RATreeView *)treeView cellForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo { // create and configure cell for *item* return cell } ``` ```objectivec - (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item { return @(index); } ``` -------------------------------- ### RATreeViewDelegate Protocol Methods (Expand/Collapse) Source: https://context7.com/fandongtongxue/ratreeview/llms.txt Implement these delegate methods to control and respond to row expansion and collapse events, allowing customization of expand/collapse behavior and handling related actions. ```APIDOC ## RATreeViewDelegate Protocol (Expand/Collapse) ### Description Implement delegate methods to control and respond to row expansion and collapse events. ### Methods #### `treeView:shouldExpandRowForItem:` - **Description**: Determines if a row should expand. Returns `YES` if the item has children, preventing expansion of items without children. - **Parameters**: - `treeView` (RATreeView *) - The tree view instance. - `item` (id) - The item being considered for expansion. - **Returns**: (BOOL) `YES` if the row should expand, `NO` otherwise. #### `treeView:shouldCollapaseRowForItem:` - **Description**: Determines if a row should collapse. Currently configured to allow all rows to collapse. - **Parameters**: - `treeView` (RATreeView *) - The tree view instance. - `item` (id) - The item being considered for collapse. - **Returns**: (BOOL) `YES` if the row should collapse, `NO` otherwise. #### `treeView:willExpandRowForItem:` - **Description**: Called just before a row expands. Updates the cell's accessory type and logs the expansion event. - **Parameters**: - `treeView` (RATreeView *) - The tree view instance. - `item` (id) - The item that is about to expand. #### `treeView:didExpandRowForItem:` - **Description**: Called immediately after a row has expanded. Logs the expansion event. - **Parameters**: - `treeView` (RATreeView *) - The tree view instance. - `item` (id) - The item that has just expanded. #### `treeView:willCollapseRowForItem:` - **Description**: Called just before a row collapses. Updates the cell's accessory type and logs the collapse event. - **Parameters**: - `treeView` (RATreeView *) - The tree view instance. - `item` (id) - The item that is about to collapse. #### `treeView:didCollapseRowForItem:` - **Description**: Called immediately after a row has collapsed. Logs the collapse event. - **Parameters**: - `treeView` (RATreeView *) - The tree view instance. - `item` (id) - The item that has just collapsed. ### Request Example ```objc // In your delegate implementation - (BOOL)treeView:(RATreeView *)treeView shouldExpandRowForItem:(id)item { RADataObject *dataObject = item; return dataObject.children.count > 0; } - (BOOL)treeView:(RATreeView *)treeView shouldCollapaseRowForItem:(id)item { return YES; } - (void)treeView:(RATreeView *)treeView willExpandRowForItem:(id)item { UITableViewCell *cell = [treeView cellForItem:item]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; NSLog(@"Will expand: %@", [(RADataObject *)item name]); } - (void)treeView:(RATreeView *)treeView didExpandRowForItem:(id)item { NSLog(@"Did expand: %@", [(RADataObject *)item name]); } - (void)treeView:(RATreeView *)treeView willCollapseRowForItem:(id)item { UITableViewCell *cell = [treeView cellForItem:item]; cell.accessoryType = UITableViewCellAccessoryNone; NSLog(@"Will collapse: %@", [(RADataObject *)item name]); } - (void)treeView:(RATreeView *)treeView didCollapseRowForItem:(id)item { NSLog(@"Did collapse: %@", [(RADataObject *)item name]); } ``` ### Response #### Success Response (200) - **`shouldExpandRowForItem:`**: (BOOL) - Indicates if expansion is allowed. - **`shouldCollapaseRowForItem:`**: (BOOL) - Indicates if collapse is allowed. - **`willExpandRowForItem:`**: Void - Executed before expansion. - **`didExpandRowForItem:`**: Void - Executed after expansion. - **`willCollapseRowForItem:`**: Void - Executed before collapse. - **`didCollapseRowForItem:`**: Void - Executed after collapse. #### Response Example *These methods primarily perform actions like updating UI elements (cells) and logging, without returning specific data structures.* ``` -------------------------------- ### Programmatic Expand and Collapse Rows in RATreeView (Objective-C) Source: https://context7.com/fandongtongxue/ratreeview/llms.txt Demonstrates how to programmatically expand and collapse individual rows or all child rows recursively in RATreeView. It covers options for applying various row animations during these operations and configuring default animation styles. Dependencies include the RATreeView framework. ```objc // Expand a single item [self.treeView expandRowForItem:dataObject]; // Expand with animation [self.treeView expandRowForItem:dataObject withRowAnimation:RATreeViewRowAnimationFade]; // Expand item and all its children recursively [self.treeView expandRowForItem:dataObject expandChildren:YES withRowAnimation:RATreeViewRowAnimationAutomatic]; // Collapse a single item [self.treeView collapseRowForItem:dataObject]; // Collapse with animation [self.treeView collapseRowForItem:dataObject withRowAnimation:RATreeViewRowAnimationRight]; // Collapse item and all its children recursively [self.treeView collapseRowForItem:dataObject collapseChildren:YES withRowAnimation:RATreeViewRowAnimationLeft]; // Configure default animations for expand/collapse self.treeView.rowsExpandingAnimation = RATreeViewRowAnimationTop; self.treeView.rowsCollapsingAnimation = RATreeViewRowAnimationBottom; // Auto-expand/collapse children when parent expands/collapses self.treeView.expandsChildRowsWhenRowExpands = YES; self.treeView.collapsesChildRowsWhenRowCollapses = YES; // Available animations: // RATreeViewRowAnimationFade // RATreeViewRowAnimationRight // RATreeViewRowAnimationLeft // RATreeViewRowAnimationTop // RATreeViewRowAnimationBottom // RATreeViewRowAnimationNone // RATreeViewRowAnimationMiddle // RATreeViewRowAnimationAutomatic ``` -------------------------------- ### RATreeViewDataSource Protocol Methods Source: https://context7.com/fandongtongxue/ratreeview/llms.txt Implement these three required methods to provide the tree structure information to RATreeView, including the number of children, the child at a specific index, and the cell to display for each item. ```APIDOC ## RATreeViewDataSource Protocol (Required Methods) ### Description Implement the three required data source methods to provide tree structure information to RATreeView. ### Methods #### `treeView:numberOfChildrenOfItem:` - **Description**: Returns the number of children for a given item. If the item is `nil`, it returns the number of top-level items. - **Parameters**: - `treeView` (RATreeView *) - The tree view instance. - `item` (id) - The item whose children count is requested. `nil` for the root level. - **Returns**: (NSInteger) The number of children. #### `treeView:child:ofItem:` - **Description**: Returns the child at a specified index for a given item. If the item is `nil`, it returns the top-level item at the specified index. - **Parameters**: - `treeView` (RATreeView *) - The tree view instance. - `index` (NSInteger) - The index of the child to retrieve. - `item` (id) - The parent item. `nil` for the root level. - **Returns**: (id) The child object at the specified index. #### `treeView:cellForItem:` - **Description**: Returns the `UITableViewCell` to be displayed for a specific item. Configures the cell with item name, child count, and appropriate accessory type based on expansion state and level. - **Parameters**: - `treeView` (RATreeView *) - The tree view instance. - `item` (id) - The item for which to create a cell. - **Returns**: (UITableViewCell *) The configured table view cell. ### Request Example ```objc // In your data source implementation - (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item { if (item == nil) { return [self.data count]; } RADataObject *dataObject = item; return [dataObject.children count]; } - (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item { if (item == nil) { return [self.data objectAtIndex:index]; } RADataObject *dataObject = item; return dataObject.children[index]; } - (UITableViewCell *)treeView:(RATreeView *)treeView cellForItem:(id)item { RADataObject *dataObject = item; NSInteger level = [treeView levelForCellForItem:item]; NSInteger numberOfChildren = [dataObject.children count]; BOOL isExpanded = [treeView isCellForItemExpanded:item]; UITableViewCell *cell = [treeView dequeueReusableCellWithIdentifier:@"Cell"]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"]; } cell.textLabel.text = dataObject.name; cell.detailTextLabel.text = [NSString stringWithFormat:@"Children: %ld", (long)numberOfChildren]; cell.indentationLevel = level; cell.accessoryType = isExpanded ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone; return cell; } ``` ### Response #### Success Response (200) - **`numberOfChildrenOfItem`**: (NSInteger) - Number of children for the item. - **`child:ofItem:`**: (id) - The child object. - **`cellForItem:`**: (UITableViewCell *) - The configured cell for the item. #### Response Example *This method configures and returns a UITableViewCell. The structure is standard for UITableViewCell.* ``` -------------------------------- ### Swift RATreeView Data Source and Delegate Implementation Source: https://context7.com/fandongtongxue/ratreeview/llms.txt This Swift code implements the RATreeViewDataSource and RATreeViewDelegate protocols to display and manage a hierarchical data structure. It handles data provision, cell configuration, row selection, and row deletion. Dependencies include UIKit and RATreeView. ```swift import UIKit import RATreeView // Data model class DataObject { let name: String var children: [DataObject] init(name: String, children: [DataObject] = []) { self.name = name self.children = children } func addChild(_ child: DataObject) { children.append(child) } func removeChild(_ child: DataObject) { children = children.filter { $0 !== child } } } // View Controller class TreeViewController: UIViewController, RATreeViewDelegate, RATreeViewDataSource { var treeView: RATreeView! var data: [DataObject] = [] override func viewDidLoad() { super.viewDidLoad() // Initialize data data = createSampleData() // Setup tree view treeView = RATreeView(frame: view.bounds) treeView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") treeView.autoresizingMask = [.flexibleWidth, .flexibleHeight] treeView.delegate = self treeView.dataSource = self treeView.treeFooterView = UIView() view.addSubview(treeView) } // MARK: - RATreeViewDataSource func treeView(_ treeView: RATreeView, numberOfChildrenOfItem item: Any?) -> Int { if let item = item as? DataObject { return item.children.count } return data.count } func treeView(_ treeView: RATreeView, child index: Int, ofItem item: Any?) -> Any { if let item = item as? DataObject { return item.children[index] } return data[index] } func treeView(_ treeView: RATreeView, cellForItem item: Any?) -> UITableViewCell { let cell = treeView.dequeueReusableCell(withIdentifier: "Cell")! let dataObject = item as! DataObject let level = treeView.levelForCell(forItem: item) cell.textLabel?.text = dataObject.name cell.detailTextLabel?.text = "Children: \(dataObject.children.count)" cell.indentationLevel = level return cell } // MARK: - RATreeViewDelegate func treeView(_ treeView: RATreeView, didSelectRowForItem item: Any) { let dataObject = item as! DataObject print("Selected: \(dataObject.name)") if treeView.isCellForItemExpanded(item) { treeView.collapseRow(forItem: item, with: RATreeViewRowAnimationFade) } else { treeView.expandRow(forItem: item, with: RATreeViewRowAnimationFade) } } func treeView(_ treeView: RATreeView, commit editingStyle: UITableViewCellEditingStyle, forRowForItem item: Any) { guard editingStyle == .delete else { return } let dataObject = item as! DataObject let parent = treeView.parent(forItem: item) as? DataObject let index: Int if let parent = parent { index = parent.children.firstIndex { $0 === dataObject }! parent.removeChild(dataObject) } else { index = data.firstIndex { $0 === dataObject }! data.remove(at: index) } treeView.deleteItems(at: IndexSet(integer: index), inParent: parent, with: RATreeViewRowAnimationRight) } // MARK: - Sample Data private func createSampleData() -> [DataObject] { let phone1 = DataObject(name: "Phone 1") let phone2 = DataObject(name: "Phone 2") let phones = DataObject(name: "Phones", children: [phone1, phone2]) let laptop1 = DataObject(name: "Laptop 1") let laptop2 = DataObject(name: "Laptop 2") let computer = DataObject(name: "Computer", children: [laptop1, laptop2]) let computers = DataObject(name: "Computers", children: [computer]) return [phones, computers] } } ``` -------------------------------- ### Dynamic Row Insertion and Deletion in Ratreeview (Objective-C) Source: https://context7.com/fandongtongxue/ratreeview/llms.txt Demonstrates how to insert new rows, delete existing rows, and move items between different parent nodes in a Ratreeview. It utilizes methods like `insertItemsAtIndexes:inParent:withAnimation:` and `deleteItemsAtIndexes:inParent:withAnimation:` for row manipulation and `reloadRowsForItems:withRowAnimation:` to update the view. Supports batch updates for efficiency. ```objc #pragma mark - Dynamic Updates // Insert new items - (void)addNewChildToItem:(RADataObject *)parentItem { RADataObject *newItem = [[RADataObject alloc] initWithName:@"New Item" children:@[]]; [parentItem addChild:newItem]; // Insert at index 0 (first position) [self.treeView insertItemsAtIndexes:[NSIndexSet indexSetWithIndex:0] inParent:parentItem withAnimation:RATreeViewRowAnimationLeft]; // Refresh parent cell to update children count [self.treeView reloadRowsForItems:@[parentItem] withRowAnimation:RATreeViewRowAnimationNone]; } // Delete items - (void)deleteItem:(RADataObject *)item { RADataObject *parent = [self.treeView parentForItem:item]; NSInteger index; if (parent == nil) { // Deleting top-level item index = [self.data indexOfObject:item]; NSMutableArray *mutableData = [self.data mutableCopy]; [mutableData removeObject:item]; self.data = [mutableData copy]; } else { // Deleting child item index = [parent.children indexOfObject:item]; [parent removeChild:item]; } [self.treeView deleteItemsAtIndexes:[NSIndexSet indexSetWithIndex:index] inParent:parent withAnimation:RATreeViewRowAnimationRight]; if (parent) { [self.treeView reloadRowsForItems:@[parent] withRowAnimation:RATreeViewRowAnimationNone]; } } // Move items between parents - (void)moveItem:(RADataObject *)item fromParent:(RADataObject *)oldParent toParent:(RADataObject *)newParent { NSInteger oldIndex = [oldParent.children indexOfObject:item]; NSInteger newIndex = 0; // Insert at beginning of new parent [self.treeView moveItemAtIndex:oldIndex inParent:oldParent toIndex:newIndex inParent:newParent]; } // Batch updates for multiple changes - (void)performBatchUpdates { [self.treeView beginUpdates]; // Perform multiple insertions/deletions/moves [self.treeView insertItemsAtIndexes:[NSIndexSet indexSetWithIndex:0] inParent:nil withAnimation:RATreeViewRowAnimationFade]; [self.treeView deleteItemsAtIndexes:[NSIndexSet indexSetWithIndex:2] inParent:nil withAnimation:RATreeViewRowAnimationFade]; [self.treeView endUpdates]; } ``` -------------------------------- ### Cell Configuration and Row Height Source: https://context7.com/fandongtongxue/ratreeview/llms.txt Configure cell appearance, row heights, and indentation levels for the RATreeView. ```APIDOC ## Cell Configuration and Row Height ### Description Configure cell appearance, row heights, and indentation levels for the RATreeView. ### Methods - **`treeView:heightForRowForItem:`** - **Description**: Returns the fixed height for a row. - **Return Type**: `CGFloat` - **`treeView:estimatedHeightForRowForItem:`** - **Description**: Returns the estimated height for a row (for performance optimization on iOS 7+). - **Return Type**: `CGFloat` - **`treeView:indentationLevelForRowForItem:`** - **Description**: Returns the indentation level for a row. - **Return Type**: `NSInteger` - **`treeView:shouldIndentWhileEditingRowForItem:`** - **Description**: Determines if a row should be indented while in editing mode. - **Return Type**: `BOOL` - **`treeView:willDisplayCell:forItem:`** - **Description**: Called before a cell is displayed. Use this to apply visual styling based on hierarchy. - **`treeView:didEndDisplayingCell:forItem:`** - **Description**: Called when a cell is removed from display. Use this to clean up cell resources. ### Global Appearance Configuration - **`treeView.rowHeight`**: Sets the global row height. - **`treeView.estimatedRowHeight`**: Sets the global estimated row height. - **`treeView.separatorStyle`**: Sets the cell separator style. - **`treeView.separatorColor`**: Sets the color of the cell separator. - **`treeView.separatorInset`**: Sets the insets for the cell separator. ``` -------------------------------- ### Configure Cell Appearance and Row Height (Objective-C) Source: https://context7.com/fandongtongxue/ratreeview/llms.txt Configure cell appearance, row heights, and indentation levels for RATreeView. This includes setting fixed or estimated row heights, controlling indentation during editing, applying visual styles based on hierarchy, and global appearance settings. ```objc #pragma mark - Cell Configuration // Fixed row height - (CGFloat)treeView:(RATreeView *)treeView heightForRowForItem:(id)item { return 60.0; } // Estimated row height for performance (iOS 7+) - (CGFloat)treeView:(RATreeView *)treeView estimatedHeightForRowForItem:(id)item { return 60.0; } // Custom indentation level - (NSInteger)treeView:(RATreeView *)treeView indentationLevelForRowForItem:(id)item { // Use default behavior from tree depth return [treeView levelForCellForItem:item]; } // Control indentation during editing - (BOOL)treeView:(RATreeView *)treeView shouldIndentWhileEditingRowForItem:(id)item { return YES; } // Called before cell is displayed - (void)treeView:(RATreeView *)treeView willDisplayCell:(UITableViewCell *)cell forItem:(id)item { RADataObject *dataObject = item; NSInteger level = [treeView levelForCellForItem:item]; // Apply visual styling based on hierarchy level CGFloat brightness = 1.0 - (level * 0.1); cell.backgroundColor = [UIColor colorWithWhite:brightness alpha:1.0]; } // Called when cell is removed from display - (void)treeView:(RATreeView *)treeView didEndDisplayingCell:(UITableViewCell *)cell forItem:(id)item { // Clean up cell resources if needed } // Global appearance configuration self.treeView.rowHeight = 44.0; self.treeView.estimatedRowHeight = 44.0; self.treeView.separatorStyle = RATreeViewCellSeparatorStyleSingleLine; self.treeView.separatorColor = [UIColor lightGrayColor]; self.treeView.separatorInset = UIEdgeInsetsMake(0, 15, 0, 0); ``` -------------------------------- ### Accessing Items and Cells Source: https://context7.com/fandongtongxue/ratreeview/llms.txt Query the tree view for items, cells, and hierarchy information. ```APIDOC ## Accessing Items and Cells ### Description Query the tree view for items, cells, and hierarchy information. ### Methods - **`cellForItem:`** - **Description**: Returns the `UITableViewCell` for a given item. - **Parameters**: - `item` (id) - The item to find the cell for. - **Return Type**: `UITableViewCell` - **`itemForCell:`** - **Description**: Returns the item associated with a given cell. - **Parameters**: - `cell` (UITableViewCell) - The cell to find the item for. - **Return Type**: `id` - **`itemForRowAtPoint:`** - **Description**: Returns the item located at a specific point in the tree view. - **Parameters**: - `point` (CGPoint) - The point to check. - **Return Type**: `id` - **`itemsForRowsInRect:`** - **Description**: Returns an array of items whose rows fall within a specified rectangle. - **Parameters**: - `rect` (CGRect) - The rectangle to check. - **Return Type**: `NSArray` - **`visibleCells`** - **Description**: Returns an array of all currently visible cells. - **Return Type**: `NSArray` - **`itemsForVisibleRows`** - **Description**: Returns an array of items corresponding to the visible rows. - **Return Type**: `NSArray` - **`parentForItem:`** - **Description**: Returns the parent item of a given item. - **Parameters**: - `item` (id) - The item whose parent to find. - **Return Type**: `id` - **`levelForCellForItem:`** - **Description**: Returns the hierarchy level of an item (0 is the root level). - **Parameters**: - `item` (id) - The item to get the level for. - **Return Type**: `NSInteger` - **`levelForCell:`** - **Description**: Returns the hierarchy level of a cell. - **Parameters**: - `cell` (UITableViewCell) - The cell to get the level for. - **Return Type**: `NSInteger` - **`isCellForItemExpanded:`** - **Description**: Checks if the cell for a given item is currently expanded. - **Parameters**: - `item` (id) - The item to check. - **Return Type**: `BOOL` - **`isCellExpanded:`** - **Description**: Checks if a given cell is currently expanded. - **Parameters**: - `cell` (UITableViewCell) - The cell to check. - **Return Type**: `BOOL` - **`numberOfRows`** - **Description**: Returns the total number of visible rows in the tree view. - **Return Type**: `NSInteger` ``` -------------------------------- ### RATreeViewDelegate: Control Expand/Collapse Behavior (Objective-C) Source: https://context7.com/fandongtongxue/ratreeview/llms.txt Implement these delegate methods to manage row expansion and collapse events in RATreeView. This includes deciding whether a row should expand or collapse, and responding to expansion/collapse actions by updating the UI and logging events. ```objc #pragma mark - RATreeViewDelegate - Expand/Collapse // Control whether a row should expand - (BOOL)treeView:(RATreeView *)treeView shouldExpandRowForItem:(id)item { RADataObject *dataObject = item; // Only allow expansion if item has children return dataObject.children.count > 0; } // Control whether a row should collapse - (BOOL)treeView:(RATreeView *)treeView shouldCollapaseRowForItem:(id)item { return YES; // Allow all rows to collapse } // Called before a row expands - (void)treeView:(RATreeView *)treeView willExpandRowForItem:(id)item { UITableViewCell *cell = [treeView cellForItem:item]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; NSLog(@"Will expand: %@", [(RADataObject *)item name]); } // Called after a row expands - (void)treeView:(RATreeView *)treeView didExpandRowForItem:(id)item { NSLog(@"Did expand: %@", [(RADataObject *)item name]); } // Called before a row collapses - (void)treeView:(RATreeView *)treeView willCollapseRowForItem:(id)item { UITableViewCell *cell = [treeView cellForItem:item]; cell.accessoryType = UITableViewCellAccessoryNone; NSLog(@"Will collapse: %@", [(RADataObject *)item name]); } // Called after a row collapses - (void)treeView:(RATreeView *)treeView didCollapseRowForItem:(id)item { NSLog(@"Did collapse: %@", [(RADataObject *)item name]); } ``` -------------------------------- ### Integrate Pull-to-Refresh with RATreeView (Objective-C) Source: https://context7.com/fandongtongxue/ratreeview/llms.txt This Objective-C code snippet shows how to integrate the standard `UIRefreshControl` with a `RATreeView` to provide pull-to-refresh functionality. It covers adding the refresh control in `viewDidLoad` and handling the refresh action by simulating a data refresh and reloading the tree view. ```objc // Add refresh control in viewDidLoad - (void)setupRefreshControl { UIRefreshControl *refreshControl = [UIRefreshControl new]; [refreshControl addTarget:self action:@selector(refreshControlChanged:) forControlEvents:UIControlEventValueChanged]; // Add to the tree view's scroll view [self.treeView.scrollView addSubview:refreshControl]; } // Handle refresh action - (void)refreshControlChanged:(UIRefreshControl *)refreshControl { // Perform data refresh dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Simulate network request [NSThread sleepForTimeInterval:1.0]; dispatch_async(dispatch_get_main_queue(), ^{ // Update data [self loadData]; [self.treeView reloadData]; [refreshControl endRefreshing]; }); }); } ``` -------------------------------- ### RATreeViewDataSource: Provide Tree Structure (Objective-C) Source: https://context7.com/fandongtongxue/ratreeview/llms.txt Implement these required methods to define the tree structure for RATreeView. This includes specifying the number of children for any given item, retrieving a specific child at an index, and configuring the UITableViewCell for each item. ```objc #pragma mark - RATreeViewDataSource // Return number of children for an item (nil = root level) - (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item { if (item == nil) { // Root level - return number of top-level items return [self.data count]; } // Return number of children for this item RADataObject *dataObject = item; return [dataObject.children count]; } // Return the child at specified index for an item - (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item { if (item == nil) { // Root level - return top-level item at index return [self.data objectAtIndex:index]; } // Return child at index for this item RADataObject *dataObject = item; return dataObject.children[index]; } // Return the cell for a specific item - (UITableViewCell *)treeView:(RATreeView *)treeView cellForItem:(id)item { RADataObject *dataObject = item; // Get hierarchical level for indentation NSInteger level = [treeView levelForCellForItem:item]; NSInteger numberOfChildren = [dataObject.children count]; BOOL isExpanded = [treeView isCellForItemExpanded:item]; // Dequeue reusable cell UITableViewCell *cell = [treeView dequeueReusableCellWithIdentifier:@"Cell"]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"]; } // Configure cell cell.textLabel.text = dataObject.name; cell.detailTextLabel.text = [NSString stringWithFormat:@"Children: %ld", (long)numberOfChildren]; cell.indentationLevel = level; cell.accessoryType = isExpanded ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone; return cell; } ``` -------------------------------- ### Implement Swipe-to-Delete and Editing in RATreeView (Objective-C) Source: https://context7.com/fandongtongxue/ratreeview/llms.txt This Objective-C code demonstrates how to enable editing mode for rows in a RATreeView, allowing for swipe-to-delete gestures and custom edit actions. It includes data source and delegate methods for handling editing styles, confirmation buttons, and editing lifecycle events. ```objc #pragma mark - Editing Support // Data source method for commit editing - (void)treeView:(RATreeView *)treeView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowForItem:(id)item { if (editingStyle == UITableViewCellEditingStyleDelete) { RADataObject *dataObject = item; RADataObject *parent = [treeView parentForItem:item]; NSInteger index; if (parent == nil) { index = [self.data indexOfObject:item]; NSMutableArray *mutableData = [self.data mutableCopy]; [mutableData removeObject:item]; self.data = [mutableData copy]; } else { index = [parent.children indexOfObject:item]; [parent removeChild:dataObject]; } [treeView deleteItemsAtIndexes:[NSIndexSet indexSetWithIndex:index] inParent:parent withAnimation:RATreeViewRowAnimationRight]; } } // Allow editing for all rows - (BOOL)treeView:(RATreeView *)treeView canEditRowForItem:(id)item { return YES; } // Delegate methods for editing - (UITableViewCellEditingStyle)treeView:(RATreeView *)treeView editingStyleForRowForItem:(id)item { return UITableViewCellEditingStyleDelete; } - (NSString *)treeView:(RATreeView *)treeView titleForDeleteConfirmationButtonForRowForItem:(id)item { return @"Remove"; } - (void)treeView:(RATreeView *)treeView willBeginEditingRowForItem:(id)item { NSLog(@"Will begin editing: %@", [(RADataObject *)item name]); } - (void)treeView:(RATreeView *)treeView didEndEditingRowForItem:(id)item { NSLog(@"Did end editing: %@", [(RADataObject *)item name]); } // Toggle editing mode - (void)editButtonTapped:(id)sender { [self.treeView setEditing:!self.treeView.isEditing animated:YES]; } // Edit actions (iOS 8+) - (NSArray *)treeView:(RATreeView *)treeView editActionsForItem:(id)item { UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { // Handle delete }]; UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Edit" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { // Handle edit }]; return @[deleteAction, editAction]; } ``` -------------------------------- ### Add Pull to Refresh Control to RATreeView (Objective-C) Source: https://github.com/fandongtongxue/ratreeview/blob/master/README.md This code snippet shows how to add a standard `UIRefreshControl` to a `RATreeView` to enable pull-to-refresh functionality. It initializes the refresh control, adds a target for value changes, and appends it as a subview to the tree view's scroll view. No external dependencies beyond UIKit are required. ```objc UIRefreshControl *refreshControl = [UIRefreshControl new]; [refreshControl addTarget:self action:@selector(refreshControlChanged:) forControlEvents:UIControlEventValueChanged]; [treeView.scrollView addSubview:refreshControl]; ``` -------------------------------- ### Scrolling and Scroll Position Source: https://context7.com/fandongtongxue/ratreeview/llms.txt Control scrolling behavior and scroll to specific items within the RATreeView. ```APIDOC ## Scrolling and Scroll Position ### Description Control scrolling behavior and scroll to specific items within the RATreeView. ### Methods - **`scrollToRowForItem:atScrollPosition:animated:`** - **Description**: Scrolls the tree view to make the row for a specific item visible. - **Parameters**: - `item` (id) - The item to scroll to. - `scrollPosition` (RATreeViewScrollPosition) - The desired scroll position. - `animated` (BOOL) - Whether the scroll should be animated. - **`scrollToNearestSelectedRowAtScrollPosition:animated:`** - **Description**: Scrolls the tree view to make the nearest selected row visible. - **Parameters**: - `scrollPosition` (RATreeViewScrollPosition) - The desired scroll position. - `animated` (BOOL) - Whether the scroll should be animated. ### Scroll Positions - `RATreeViewScrollPositionNone` - `RATreeViewScrollPositionTop` - `RATreeViewScrollPositionMiddle` - `RATreeViewScrollPositionBottom` ### Accessing Underlying Scroll View - **`treeView.scrollView`** - **Description**: Provides access to the underlying `UIScrollView` for advanced scroll control. ``` -------------------------------- ### Control Scrolling Behavior (Objective-C) Source: https://context7.com/fandongtongxue/ratreeview/llms.txt Control scrolling behavior and scroll to specific items within the RATreeView. This includes scrolling to a particular item, to the nearest selected row, and accessing the underlying UIScrollView for advanced control. ```objc #pragma mark - Scrolling // Scroll to a specific item [self.treeView scrollToRowForItem:dataObject atScrollPosition:RATreeViewScrollPositionTop animated:YES]; // Scroll to nearest selected row [self.treeView scrollToNearestSelectedRowAtScrollPosition:RATreeViewScrollPositionMiddle animated:YES]; // Available scroll positions: // RATreeViewScrollPositionNone - No scrolling // RATreeViewScrollPositionTop - Scroll to top of visible area // RATreeViewScrollPositionMiddle - Scroll to middle of visible area // RATreeViewScrollPositionBottom - Scroll to bottom of visible area // Access the underlying scroll view for advanced scroll control UIScrollView *scrollView = self.treeView.scrollView; scrollView.contentOffset = CGPointMake(0, 100); scrollView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0); scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(64, 0, 49, 0); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.