Giter Site home page Giter Site logo

treeview's Introduction

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

  • (void)expand:(NSIndexPath *)treeIndexPath;
  • (BOOL)isExpanded:(NSIndexPath *)treeIndexPath;
  • (void)collapse:(NSIndexPath *)treeIndexPath;

Instead of implementing UITableViewDataSource in your controller - change it to TreeTableDataSource. TreeTableDataSource protocol extends UITableViewDataSource by introducing 2 new methods:
@required

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

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:

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)treeIndexPath;

and use - (NSIndexPath *)tableIndexPathFromTreePath:(NSIndexPath *)treeIndexPath 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 indexPath with:

  • (NSIndexPath *)treeIndexPathFromTablePath:(NSIndexPath *)treeIndexPath;
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.

treeview's People

Contributors

genkernel avatar smartch avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.