How to build tree structure using native iOS Table View UI
POSTED ON
May 16, 2014
POSTED BY
Aayan Arif
POSTED ON May 16, 2014
POSTED BY Aayan Arif
SHARE
native iOS- requirement of ‘meetings and to-do tasks’ application for iPad. Besides the basic to-do tasks functionality
Our, native iOS client came with a requirement of ‘meetings and to-do tasks’ application for iPad. Besides the basic to-do tasks functionality, this application had a complex table view UI requirements:
The task list should be organized in a hierarchical nested list structure.
It should also have expand/collapse functionality with smooth sliding animation.
Users should also be able to create new items at any position.
Users should sort items by moving an item from the original position to any other position.
JavaScript to Objective-C:
The Parent-Child list-like functionality was already working in a web application. The HTML tags, <ul>, <li> provide an excellent Parent-Child relationship among visual components. With the help of jQuery plugins, it is a piece of cake to organize components in a nested list order.
Unfortunately, there was no such Parent-Child relationship available in Apple’s iOS SDK. The only component to draw a listing is UITableView but it doesn’t provide any nested list-like structure.
Implementation Ideas:
First of all we looked for some open sources for Tree like view implementation but due following limitations it couldn’t become a solution. Limitations: Unfortunately none of them was providing functionality like; re-ordering and creating new items at specific positions.
Another possibility was to create this structure from scratch using another component provided by Apple’s iOS SDK; i.e: UI Table View. The UIView component is similar to <div> element tag in HTML, but in addition the UIView component is capable of containing other child UIView components. Adding new child components is easy.Limitations: Idea of implementing above requirement from scratch was good in a sense that it was now possible to achieve our goal, but creating a list structure from scratch was time consuming when adjusting each UIView’s placement in vertical order.
The UIView components are not automatically positioned when some other UIView components are added or deleted. In a vertical listing of 5 UIView components, if a new component is added at position 2, we have to make many updates in table view UI:
Manually calculate the area of newly added item to place it at position 2 (adjusting width, height, x, y)
Shift next components at position 3, 4, 5 to bottom (increase there y positions as much as the height of newly created item)
Adjust overall page scrolling (so that if bottom components go out of screen they can accessed by scrolling)
Same tedious calculation was needed when some item was deleted.
Solution Implemented:-native iOS
1- Maintaining Hierarchy:
After trying many possibilities, we finally decided not to reinvent the wheel. We used iOS built-in UITableView component which at least provides a single nested level list structure with automatic scroll handling. But this time instead of completely depending on this UI component, we created a data structure that keeps track of each Parent-Child relationship among data items.
Some of its responsibilities provide the following information about data items:
(BOOL)isRoot;
(BOOL)isChild;
(BOOL)hasChildren;
(int)numberOfChildren;
(int)indentationLevel;
(BOOL)isNodeFirstChild;
(BOOL)isNodeLastChild;
(MKTreeNode *)getNextSiblingForNode;
(MKTreeNode *)getPreviousSiblingForNode;
(MKTreeNode *)getFirstChildOfNode;
(MKTreeNode *)getLastChildOfNode;
The data structure we created provides all the information that we need to display an item and compute our custom logics on it. So now we are just using a single nested level UITableView component. While displaying data items on the list, we check if it is a child item, we just indent all contents in it to the right with a fixed ‘x’ offset value, giving it a look of a child item.
2- Achieving Expand/Collapse functionality:
The second part of the requirement was the expand/collapse functionality, which again UITableView doesn’t provide. For this, we got the idea from some open sources. The basic idea was to remove the child data items with sliding animation in case of collapse. When we need to expand it, we just add data items with sliding animation.
We managed a two dimensional temporary array, which was responsible for getting data from data structure and displaying it on screen. To maximize the adding/removing performance, we are doing remove/add operations on this two dimensional array.
3- Reorder items/Sorting:
The requirement of re-ordering of items also becomes easy with our handy data structure. Just remove children from previous parent items, and add them to the new parent. After that, redraw the list using UI Table View’s built-in method i.e.:
[treeTableView reloadData];
By removing the default separator lines, we got a nested list like view:
One of the essential skills every Python programmer should have is the ability to run Python scripts in a terminal. In this comprehensive guide, we’ll cover various ways to run Python scripts in a terminal and explore different scenarios, including running Python on Windows and executing Python scripts in Linux. Whether you’re a developer at
Routers play a pivotal role in segmenting and managing traffic. They are the guardians of data flow, separating and directing it to its intended destination. A fundamental concept in networking is the creation of broadcast domains, which are distinct areas within a network where broadcast traffic is contained. In this blog, we will explore how
Having a dual monitor setup can significantly enhance your productivity, allowing you to multitask efficiently and work on multiple tasks simultaneously. However, encountering the issue of both monitors displaying the same content can be frustrating and hinder your ability to take full advantage of the dual monitor setup. In this blog post, we will explore
This article throws some light on working with Core Data background threads as it is not documented in any of Apple’s Core Data guide: Requirement and Idea: In one of our existing iPad application, we had to implement offline feature that requires storing all data in device’s local storage. We were using Apple’s Core Data,
In this article, we will explore how to add fonts to Google Docs, including custom fonts, and also discuss how to add fonts to Google Slides for added creativity. Additionally, we’ll cover how to access the Extensis Fonts add-on to expand your font choices even further. Let’s dive in! How to Add Fonts to Google
ABOUT THE AUTHOR
Aayan Arif
Content Strategist at vteams - Aayan has over 8 years of experience of working with multiple industries.
0 Comments