Skip to content

gringoireDM/LNZTreeView

Repository files navigation

Build Status codecov Swift

LNZTreeView

This is a swift implementation for iOS of a Tree View. A Tree View is a graphical representation of a tree. Each element (node) can have a number of sub elements (children).

This particular implementation of TreeView organizes nodes and subnodes in rows and each node has an indentation that indicates the hierarchy of the element. A parent can be expanded or collapsed and each children can be a parent itself containing more sub nodes.

LNZTreeView

Basic Principles

The LNZTreeView is a view built on top of UITableView. It proxies UITableView's delegate and datasource in order to have a hierarchical usage of those APIs.

It can work with any entity conforming the protocol TreeNode.

Cells

Just like for UITableView it is possible to use Custom Cells in a LNZTreeView. You can register a cell class to be used for a specific cell Identifier by using the methods register(_ cellClass: AnyClass?, forCellReuseIdentifier identifier: String) and register(_ nib: UINib?, forCellReuseIdentifier identifier: String).

Just like for UITableView it is possible to obtain an UITableViewCell instance of the type previously specified with the register methods by using the dequeueReusableCell(withIdentifier identifier: String, for indexPath: IndexPath) -> UITableViewCell and dequeueReusableCell(withIdentifier identifier: String) -> UITableViewCell? methods.

Delegate and DataSource

The DataSource is the responsible of populating the TreeView according with your set of data. For efficiency, the LNZTreeView will query its data source if needed to get the right info to render the rows. All the indexPaths in the data source's methods are relative to the parameter parentNode.

For example:

root
--- element A
--- --- sub element A
--- --- sub element B
--- --- sub element C
--- element B

In this case an IndexPath with row 1 in parentNode equals to element A, will represent sub element B. The section of IndexPath just like for UITableView represent the index of the section to be rendered. If parentNode is nil, then the indexPath is relative to the root.

Example:

root A
--- element AA
--- --- sub element AA
--- --- sub element AB
--- --- sub element AC
--- element AB
root B

In this case an IndexPath with row 1 in parentNode equals to nil, will represent root B.

All the indexPaths in the delegate works following the same logic.

If selected a node will automatically expand if it is expandable or it will collapse if already expanded. The delegate will be aknowledged of this events by the methods treeView(_ treeView: LNZTreeView, didExpandNodeAt indexPath: IndexPath, forParentNode parentNode: TreeNode?) and treeView(_ treeView: LNZTreeView, didCollapseNodeAt indexPath: IndexPath, forParentNode parentNode: TreeNode?). If the node is not expandable (or collapsable) the method treeView(_ treeView: LNZTreeView, didSelectNodeAt indexPath: IndexPath, forParentNode parentNode: TreeNode?) will be called.

Install

You can install LNZTreeView via cocoapods

use_frameworks!
pod 'LNZTreeView'