Click here to Skip to main content
14,039,595 members
Click here to Skip to main content
Add your own
alternative version

Stats

16.7K views
492 downloads
4 bookmarked
Posted 22 May 2016
Licenced CPOL

Tree View in Swift For iOS

, 22 May 2016
Rate this:
Please Sign up or sign in to vote.
Tree View in Swift for iOS that is easy to implement and use

Introduction

I love the TreeView in .NET and am wanting one for an iPad app, but discovered that iOS does not have a TreeView control so after hunting around, I just decided to write one of my own.

Background

I did find a few iOS Tree Views on various sites, but they were all old and used Objective-C with no clear instructions on adding to your project, so that was just not going to work out.

However, I did find a fairly easy one by Ahmed Karim written in Objective-C that I downloaded and looked at and used some of his ideas.

Ahmed Karim's project => This is A Tree Control That Works on iOS Devices

Using the Code

I started with the Single View Application in XCode. I added a tableView to the main View Controller and then set the attributes to use TreeViewController.

I also created a custom cell and set that to use TreeViewCell.

screen shot

The Data for the Tree is loaded in TreeLists.swift. It is an array of a class called TreeViewData. Be sure to use a unique id for each one and then for the children nodes, set the parentId to the id of the node they belong under. Set the Zero level parentId to something that is not going to be used.

static func LoadInitialData() -> [TreeViewData]
    {
        var data: [TreeViewData] = []
        data.append(TreeViewData(level: 0, name: "cindy's family tree", id: "1", parentId: "-1")!)
        data.append(TreeViewData(level: 0, name: "jack's family tree", id: "2", parentId: "-1")!)
        data.append(TreeViewData(level: 1, name: "katherine", id: "3", parentId: "1")!)
        data.append(TreeViewData(level: 1, name: "kyle", id: "4", parentId: "1")!)
        data.append(TreeViewData(level: 2, name: "hayley", id: "5", parentId: "3")!)
        data.append(TreeViewData(level: 2, name: "macey", id: "6", parentId: "3")!)
        data.append(TreeViewData(level: 1, name: "katelyn", id: "7", parentId: "2")!)
        data.append(TreeViewData(level: 1, name: "jared", id: "8", parentId: "2")!)
        data.append(TreeViewData(level: 1, name: "denyee", id: "9", parentId: "2")!)
        data.append(TreeViewData(level: 2, name: "cayleb", id: "10", parentId: "4")!)
        data.append(TreeViewData(level: 2, name: "carter", id: "11", parentId: "4")!)
        data.append(TreeViewData(level: 2, name: "braylon", id: "12", parentId: "4")!)
        data.append(TreeViewData(level: 3, name: "samson", id: "13", parentId: "5")!)
        data.append(TreeViewData(level: 3, name: "samson", id: "14", parentId: "6")!)     

        return data
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

pgmr_64804
Systems Engineer
United States United States
I Graduated with a Computer Information Science Degree in 1990 and have been working developing systems since then.

I started in COBOL, moved to PROGRESS, did a little RPG, then moved on to Mobile apps and desktop applications.

It's been a great journey Smile | :)

You may also be interested in...

Comments and Discussions

 
QuestionCustom Cell Pin
Member 141103337-Jan-19 2:58
memberMember 141103337-Jan-19 2:58 
QuestionSwift 3.0 Pin
Member 1306195516-Mar-17 21:33
memberMember 1306195516-Mar-17 21:33 
BugImages are missing Pin
George Jonsson22-May-16 19:28
professionalGeorge Jonsson22-May-16 19:28 
GeneralRe: Images are missing Pin
pgmr_6480422-May-16 22:49
memberpgmr_6480422-May-16 22:49 
GeneralRe: Images are missing Pin
George Jonsson22-May-16 22:55
professionalGeorge Jonsson22-May-16 22:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190425.1 | Last Updated 23 May 2016
Article Copyright 2016 by pgmr_64804
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid