Giter Club home page Giter Club logo

imtreeview's Introduction

IMTreeView

[CI Status](https://travis-ci.org/Ian McDowell/IMTreeView) Version License Platform

screenshot

IMTreeView is a simple library that allows you to display a tree structure with any UITableView. It is simple, well-tested and documented, and written entirely in Swift.

Take a look at the example project to see it in action!

Requirements

IMTreeView is written in Swift 2, so it requires Xcode 7.

Installation

IMTreeView is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "IMTreeView"

Usage

To run the example project, clone the repo, and run pod install from the Example directory first.

  1. Add the following import to the top of your source file:
import IMTreeView
  1. Add a UITableView to the view controller, either through the storyboard, or in code. Set the tableView's dataSource to be the UIViewController.

  2. Make the UIViewController implement the UITableViewDataSource and IMTreeViewDataSource protocols.

  3. Implement the required methods as shown:

// MARK: UITableViewDataSource

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1  // or however many you would like to
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    // this allows the tableView to function as a tree
    return tableView.numberOfItemsInSection(section)
    
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
    // convert the tableView indexPath to a tree-based indexPath
    let treeIndexPath = tableView.treeIndexPathFromTablePath(indexPath)
    
    // retrieve the data to display
    let node = self.nodeAtIndexPath(treeIndexPath)
    
    // to determine indentation
    let level = treeIndexPath.length - 2
    
    // get a cell
    let cell = tableView.dequeueReusableCellWithIdentifier("identifier", forIndexPath: indexPath)
    
    // configure cell
    cell.textLabel?.text = node.title
    cell.indentationLevel = level
    
    return cell
}


// MARK: IMTreeViewDataSource

func tableView(tableView: UITableView, numberOfChildrenForIndexPath indexPath: NSIndexPath) -> Int {
    if indexPath.length == 1 {
        // return the number of root nodes
        return self.nodes.count
    } else {
        // return the number of children of this node
        let node = self.nodeAtIndexPath(indexPath)
        return node.children.count
    }
}

func tableView(tableView: UITableView, isCellExpandedAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true // if supporting collapsing, you should return whether this indexPath is expanded.
}


// MARK: Helpers

func nodeAtIndexPath(indexPath: NSIndexPath) -> Node {
    // see example project for implementation
}

Author

Ian McDowell, [email protected]

License

IMTreeView is available under the MIT license. See the LICENSE file for more info.

imtreeview's People

Watchers

James Cloos avatar Ze Silva 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.