Preview
Expand cells inTableViewup to ∞-1 . You can use anyCustom Cellfor anyParent,Childsor theirSubchilds
Features
Static Initialization
Static Initialization using Index
Dynamic Initialization using JSON
Custom Cell
Control of Cells
Fast scrolling, memory efficient
Well unit tested
Installation
KJExpandableTableTree is available through CocoaPods . To install it, simply add the following line to your Podfile:
pod 'KJExpandableTableTree'
Getting Started
There are 3 ways to initialize this library.
You can choose any either way to create tree. Static/Dynamic.
1 – Dynamic tree, using JSON – initialization.
Example folder. I’ve usedTree.jsonfile forJSON Array.
{
"Tree": {
"Id": 0,
"Parents": [
{
"Id": 1,
"Children": [
{
"Id": 2,
"Children": null
},
{
"Id": 3,
"Children": [
{
"Id": 28,
"Children": [
{
"Id": 29,
"Children": null
}
]
}
]
}
]
},
{
"Id": 4,
"Children": null
}
]
}
}
InitializeKJTreewithJSON array of Parentssimilar to below, Provide required Key’s name and YOUR ARE DONE .
// KJ Tree instances -------------------------
var kjtreeInstance: KJTree? = nil
var arrayParents: NSArray?
if let treeDictionary = jsonDictionary?.object(forKey: "Tree") as? NSDictionary {
if let arrayOfParents = treeDictionary.object(forKey: "Parents") as? NSArray {
arrayParents = arrayOfParents
}
}
if let arrayOfParents = arrayParents {
kjtreeInstance = KJTree(parents: arrayOfParents, childrenKey: "Children", idKey: "Id")
}
2 – Static Tree Index Initialization.
Example folder.
// KJ Tree instances -------------------------
// You can easily create tree by Indexing.
// below, There will be 3 parents -
// 1.1 indicates 1 child inside 1st parent.
// in Second, 2nd parent have 1 child (2.1....), That 1 child have 3 subchilds (2.1.1..., 2.1.2..., 2.1.3...), now it's easy to understand 2.1.3.2 and 2.1.3.3 means 2 sub childs inside 2.1.3.
// in Third, 3rd parent have 3 childs. 1, 2, 3.
kjtreeInstance = KJTree(indices:
["1.1",
"2.1.1",
"2.1.2.1",
"2.1.3.2",
"2.1.3.3",
"3.1",
"3.2",
"3.3"]
)
3 – Static Tree Initialization.
Example folder. Swifty robust way to initialize this library
// KJ Tree instances -------------------------
var kjtreeInstance: KJTree?
// You can easily identify here, I've one parent called parent1, 3 childs inside it, 2 sub childs inside 2nd child, and 2 more sub childs inside 2nd sub child.
// You can add as many as internal level of childs hierarchy.
// I've provided a block of each parent and child, use this block to return no of childs [Child] inside parent/child.
// this will provide you a robust visibility of static tree.
let parent1 = Parent() { () -> [Child] in
let child1 = Child()
let child2 = Child(subChilds: { () -> [Child] in
let subchild1 = Child()
let subchild2 = Child(subChilds: { () -> [Child] in
let subchild1 = Child()
let subchild2 = Child(subChilds: { () -> [Child] in
let subchild1 = Child()
return [subchild1]
})
return [subchild1, subchild2]
})
return [subchild1, subchild2]
})
let child3 = Child()
return [child1, child2, child3]
}
kjtreeInstance? = KJTree(Parents: [parent1])
I’ve added 2 more parents for my demo simulation
Methods
UseUITableViewdelegates, as you guys are using, simply call libraryMethodsfrom your delegates.
numberOfRowsInSection
CalltableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> NSIntegerto return number of cells.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return kjtreeInstance.tableView(tableView, numberOfRowsInSection: section)
}
cellIdentifierUsingTableView
CallcellIdentifierUsingTableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> Nodeto receiveNodeinstance.
Usenode.indexto get index of each cell to be shown in tableview.
For parents you will receive0,1,2,…for childs0.0, 0.1, 0.2, 1.0, 1.1,….for sub childs0.0.0, 0.0.1, 0.1.0, 1.0.0, 1.1.1, ….and so one for sub childs of sub childs you will receive4 index separated by . (dot)
NOTE:You can return custom cells based on your needs, I’ve enclosed 3 examples to show you guys how you can return cells for different purpose.
Example_Static_Init- Return cell by Levels.
Example_Static_Init_Using_Index- Return cell by your assignedCustom Index
Example_Dynamic_Init- cell by Levels. You can use your given identity atnode.givenIndexoractual indexprovided by me to return different cell based on your needs.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let node = kjtreeInstance.cellIdentifierUsingTableView(tableView, cellForRowAt: indexPath)
// You can return different cells for Parents, childs, subchilds, .... as below.
let indexTuples = node.index.components(separatedBy: ".")
if indexTuples.count == 1 || indexTuples.count == 4 {
// return cell for Parents and subchilds at level 4. (For Level-1 and Internal level-4)
}else if indexTuples.count == 2{
// return cell for Childs of Parents. (Level-2)
}else if indexTuples.count == 3{
// return cell for Subchilds of Childs inside Parent. (Level-3)
}
// Return below cell for more internal levels....
var tableviewcell = tableView.dequeueReusableCell(withIdentifier: "cellidentity")
if tableviewcell == nil {
tableviewcell = UITableViewCell(style: .default, reuseIdentifier: "cellidentity")
}
tableviewcell?.textLabel?.text = node.index
tableviewcell?.backgroundColor = UIColor.yellow
tableviewcell?.selectionStyle = .none
return tableviewcell!
}
didSelectRowAt
CalltableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) -> KJExpandableTableTree.Nodeto receiveNodeinstance.
Usenodeinstance and it’sindex/givenIndexto verify specific cell press, to do additional task in your tableview’sdidSelectRowAt.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let node = kjtreeInstance.tableView(tableView, didSelectRowAt: indexPath)
print(node.index)
// if you've provided a 'Key'/'Id', you will receive it here.
print(node.keyIdentity)
// if you've added any identifier or used indexing format
print(node.givenIndex)
}