TreeView 1.1.3

TreeView 1.1.3

Maintained by kernel.



TreeView 1.1.3

  • By
  • kernel

Swift notes

TreeView is written in Objective-C and fully compatibile with Swift. Check out swift-example branch to see how well it plays with Swift.

TreeView

Component that introduces cells + subcells support for any UITableView living there in a controller's view.

Example on Youtube: http://youtu.be/zS3gQ4pnmBs

TreeView is a "proxy" object that sits between table view and a view controller, proxies all calls to data source and converts 2d-like indexPaths (0-0, 0-1, ...) into N-depth indexPaths (0-0, 0-0-1, 0-0-2, 0-1-0-1, ...).

You usually use TreeView component when your UITableViewCell wants to contain its own subcells that can be easily shown / hidden.

Examples

Take a look at 3 branches: fsTree-example, allExpanded-example and plistDatasource-example to get inside view on how to implement subcells support for a table view.

Implementation details

TreeView adds 2 logical states to every cell: expanded and collapsed.

You should expand cell to reveal its subcells.

Keeping this in mind helper methods(via UITableView category) were implemented:

func expand(treeIndexPath: IndexPath)
func isExpanded(treeIndexPath: IndexPath) -> Bool
func collapse(treeIndexPath: IndexPath)

Instead of implementing UITableViewDataSource in your controller - implement TreeTableDataSource. TreeTableDataSource protocol extends UITableViewDataSource by introducing 2 new required methods:

func tableView(_ tableView: UITableView, isCellExpandedAt treeIndexPath: IndexPath) -> Bool
func tableView(_ tableView: UITableView, numberOfSubCellsForCellAt treeIndexPath: IndexPath) -> Int

Notice all required dataSource methods are invoked with indexPath of N-depth that uniquely identify cell or subcell.
Hence you should change behaviour of the following methods:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
func tableView(tableView: UITableView, cellForRowAt treeIndexPath: IndexPath) -> UITableViewCell

and use

func tableIndexPathFromTreePath(treeIndexPath: IndexPath) -> IndexPath

if you need to convert N-depth index path into 2d index path.

On the other hand all optional methods are transparently forwarded to your implementations (if such exists) and indexPath parameter is not changed - it is 2d indexPath. You can convert it into N-depth treeIndexPath with:

func treeIndexPathFromTablePath(indexPath: NSIndexPath) -> NSIndexPath

method.

Installation

TreeView is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'TreeView'

Conclusion

With TreeView you can have any cells-subcells levels number. For example:
Cells levels and its indexPaths representation:

  • section 0
    • [0, 0]
    • [0, 1]
    • [0, ...]
  • section 1
    • [1, 0]
    • [1, 1]
      • [1, 1, 0]
      • [1, 1, 1]
      • [1, 1, ...]
    • [1, 2]
      • [1, 2, 0]
  • section 2
    • [2, 0]
    • [2, 1]
    • [2, 2]
    • [2, 3]
    • [2, ...]
  • [...]


With UITableView data srtucture exposed via 2d indexPaths only. For example: - section 0 - [0, 1] - [0, 2] - [0, 3] - [0, ...] - section 1 - [1, 0] - [1, 1] - [1, 2] - [1, ...] - section ...

See demo app example that represents this concept in action.

Concept

TODO

* Test cells moving between sections.