/TreeView

UITableView with cells + subcells support.

Primary LanguageObjective-C

TreeView

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

ExampleApp demo on Youtube: http://youtu.be/zS3gQ4pnmBs

TreeView is a "proxy" object that 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.

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 were implemented:

  • (void)expand:(NSIndexPath *)indexPath;
  • (BOOL)isExpanded:(NSIndexPath *)indexPath;
  • (void)close:(NSIndexPath *)indexPath;

Instead of implementing UITableViewDataSource in your controller - change it to TreeTableDataSource. TreeTableDataSource protocol inherits UITableViewDataSource and introduces 2 new methods:
@required

  • (BOOL)tableView:(UITableView *)tableView isCellExpanded:(NSIndexPath *)indexPath;
  • (NSUInteger)tableView:(UITableView *)tableView numberOfSubCellsForCellAtIndexPath:(NSIndexPath *)indexPath;

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

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
and ensure that it fills every cell with appropriate data.

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

  • (NSIndexPath *)treeIndexPathFromTablePath:(NSIndexPath *)indexPath;
method.

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.