genkernel/TreeView


"TreeView - sub-cells simplified" (c). Enable subcells in UITableView with a single drop-in extension. CocoaPod:

http://cocoapods.org/pods/treeview

License: MIT

Language: Objective-C

Keywords: collapsable, collapse, concept, expand, expandable, expandablelistview, expandabletableview, indexpath, ios, nsindexpath, objective-c, subtree, swift, tree-node, treeview, uitableview, uitableviewcell, uitableviewdatasource


TreeView is iOS single-class component written in ObjC that enables cell + subcells support for UITableView-s.

  • Project status
  • Examples
  • Documentation

Current status

TreeView was initially written in 2012 and has been used in multiple projects since then. Development is finalized, component and its public API is considered stable.

The most convenient way to use it is via CocoaPods:

pod 'TreeView'

Aletrnativelly you may simply drop TreeView/TreeTable.h,m into your project.

Examples

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

You may find demo iOS app with 3 working modules in 'Examples' directory:

  • inSwift-4.2
  • fileSystem-asTree
  • plist-allExpanded-byDefault

Documentation

In basic MVC scenario ViewController is set as DataSource of UITableView.

TreeView package introduces single new class: TreeTable. It is designed to sit in between ViewController and UITableView as a DataSource object that knows how to work with subcells via deeper "nested" indexPaths.

TreeTable implements UITableViewDataSource protocol and represents inner subcells with indexPaths of deeper levels. For instance cell at 0-0 indexPath may contain 3 subcells: 0-0-0, 0-0-1, 0-0-2.

In plan MVC example: TreeTable is a "proxy" object that sits between tableView and a viewController, 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, ...) to represent subcells.

You usually decide to use TreeTable component when your UITableViewCell wants to contain its own subcells that can be easily expanded or collapsed.

Implementation details

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

You should expand a cell to reveal its subcells.

Keeping this in mind helper methods(as 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 you provide any) 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.

Concept image

Concept

Project Statistics

Sourcerank 7
Repository Size 961 KB
Stars 46
Forks 10
Watchers 2
Open issues 0
Dependencies 0
Contributors 1
Tags 5
Created
Last updated
Last pushed

Top Contributors See all

Anthony

Packages Referencing this Repo

TreeView
Sub-cells simplified. ©
Latest release 1.1.3 - Updated - 46 stars

Recent Tags See all

1.1.3 August 22, 2018
1.1.2 July 12, 2018
1.1.1 July 12, 2018
1.1.0 May 23, 2018
v1.0 March 30, 2016

Interesting Forks See all

smartch/TreeView
UITableView with cells + subcells support.
Objective-C - Last pushed - 1 stars

Something wrong with this page? Make a suggestion

Last synced: 2018-06-28 16:26:48 UTC

Login to resync this repository