Code Monkey home page Code Monkey logo

treeview's Introduction

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

treeview's People

Contributors

genkernel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

treeview's Issues

How can I resize hight Row?

I try to use this

  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.length >= 4) {
    return 200;
    }
    return 50;
    }
  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 ) {
    return 200;
    }
    return 50;
    }

there are work only all cell were changed.

<NSIndexPath: 0xc000000000000026> {length = 4, path = 0 - 0 - 0 - 0}
<NSIndexPath: 0xc000000001000026> {length = 4, path = 0 - 0 - 1 - 0}

I try to use certain cell to change hight row, but I could't.
Please let me now How can I change cell's hight?

Get index path for a specific item

What is the best way to get the tree index path for an item?

I looked through the code and the examples for a method like -(NSIndexPath *)indexPathForItem:(NSDictionary *)item;

Exception when accessing index path row

Hey,

Thanks again for creating this, it's been very helpful.

I have some code to collapse all the currently expanded cells in the table:

    for (NSIndexPath *indexPath in self.expandedItems.allKeys) {
        NSIndexPath *treeIndexPath = [self.tableView treeIndexPathFromTablePath:indexPath];
        [self.expandedItems removeObjectForKey:treeIndexPath];
        [self.tableView collapse:treeIndexPath];
    }

In - (NSIndexPath *)treeIndexPathFromTablePath:(NSIndexPath *)indexPath, the app crashes at
`NSUInteger row = indexPath.row;```, with this exception:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid index path for use with UITableView.  Index paths passed to table view must contain exactly two indices specifying the section and row.  Please use the category on NSIndexPath in UITableView.h if possible.'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010c3bf3f5 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010c040bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010c3bf25a +[NSException raise:format:arguments:] + 106
    3   Foundation                          0x000000010bc5e28f -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
    4   UIKit                               0x000000010ac3f6d9 -[NSIndexPath(UITableView) row] + 175
    5   TreeViewExample                     0x000000010a3a525c -[UITableView(TreeTable) treeIndexPathFromTablePath:] + 92
    6   TreeViewExample                     0x000000010a3a15b9 -[DAViewController collapse] + 521
    7   Foundation                          0x000000010bbfafd4 __NSFireTimer + 83
    8   CoreFoundation                      0x000000010c3274e4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    9   CoreFoundation                      0x000000010c3270a5 __CFRunLoopDoTimer + 1045
    10  CoreFoundation                      0x000000010c2ea3dd __CFRunLoopRun + 1901
    11  CoreFoundation                      0x000000010c2e9a06 CFRunLoopRunSpecific + 470
    12  GraphicsServices                    0x000000010edf99f0 GSEventRunModal + 161
    13  UIKit                               0x000000010a8fe550 UIApplicationMain + 1282
    14  TreeViewExample                     0x000000010a3a1063 main + 115
    15  libdyld.dylib                       0x000000010c9ec145 start + 1
    16  ???                                 0x0000000000000001 0x0 + 1
)

It seems to only happen when the depth of the expanded items is > 1.

Migration: ObjC ---> Swift

Even thought TreeView can be accessed from Swift via bridging header - it is implemented in ObjC. It would be nice to rewrite existing code into native Swift to simplify integration with other Swift-based projects.

Use without Storyboard

Hi,

How can this be used without a Storyboard?
Tried to add the tree in code, but must have been doing it wrong.

No Example Visible

Hello,
I just came across this awesome library for which my requirements meet.
But I am not able to implement it, as there is no working example available on any site, also the example links that you have provided only show a 404 error. Can you provide a simple tutorial on how to use this?

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.