Skip to content

carabina/TreeView

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

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.

About

Enable cells + subcells in UITableView with single extension.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Objective-C 91.6%
  • Ruby 8.4%