A swift TreeView
Switch branches/tags
Nothing to show
Clone or download
Latest commit 7796bef Jun 13, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
LNZTreeView Added Possibility to customize row height for each node. Jun 12, 2018
LNZTreeViewDemo.xcodeproj using old build system Jun 12, 2018
LNZTreeViewDemo Version bump Jun 12, 2018
LNZTreeViewTests Added demo Nov 8, 2017
.gitignore Create .gitignore Nov 7, 2017
.swift-version Added podspec file Nov 8, 2017
.travis.yml Update .travis.yml Nov 13, 2017
LICENSE Create LICENSE Nov 7, 2017
LNZTreeView.gif Replaced image Nov 7, 2017
LNZTreeView.podspec Version bump Jun 12, 2018
README.md Update README.md Nov 14, 2017


Build Status codecov Swift


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.


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.


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:

--- 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.


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.


You can install LNZTreeView via cocoapods

pod 'LNZTreeView'